Solved

Microsoft.XMLHTTP sending a file

Posted on 2004-09-15
14
28,361 Views
Last Modified: 2012-05-07
Hi I am currently re-developing a web based front end for an estate agent to manage their propertys. I have decided to use a Microsoft.XMLHTTP object to communicate between the client and server. My problem is got to do with sending a file with this. The client is composed of the usual GUI components which the data can be easily retrieved from for sending to the server but it also contains a file element for selecting a picture of the property to be uploaded. How do I get the data from this so that it can be uploaded to the server using the Microsoft.XMLHTTP object. I have found a bit of code on the net but it is no use to me because it would involve changing the security in "Internet Options" which I have no control over.

<HTML>
<HEAD><TITLE>File Send</TITLE></HEAD>
<BODY>
   <INPUT id=btn_send name="btn_send" type=button value="FILE SEND">
   <DIV id=div_message>Ready</DIV>
</BODY>
</HTML>

<SCRIPT LANGUAGE=JavaScript>

// files upload function
function btn_send.onclick()
{
   // create ADO-stream Object
   var ado_stream = new ActiveXObject("ADODB.Stream");

   // create XML document with default header and primary node
   var xml_dom = new ActiveXObject("MSXML2.DOMDocument");
   xml_dom.loadXML('<?xml version="1.0" ?> <root/>');
   // specify namespaces datatypes
   xml_dom.documentElement.setAttribute("xmlns:dt", "urn:schemas-microsoft-com:datatypes");

   // create a new node and set binary content
   var l_node1 = xml_dom.createElement("file1");
   l_node1.dataType = "bin.base64";
   // open stream object and read source file
   ado_stream.Type = 1;  // 1=adTypeBinary
   ado_stream.Open();
   ado_stream.LoadFromFile("c:\\sold.gif");
   // store file content into XML node
   l_node1.nodeTypedValue = ado_stream.Read(-1); // -1=adReadAll
   ado_stream.Close();
   xml_dom.documentElement.appendChild(l_node1);

   // we can create more XML nodes for multiple file upload

   // send XML documento to Web server
   var xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
   xmlhttp.open("POST","http://www.google.com",false);
   xmlhttp.send(xml_dom);
   // show server message in message-area
   div_message.innerHTML = xmlhttp.ResponseText;
}
</SCRIPT>


thanks
John.
0
Comment
Question by:mikevandike
  • 6
  • 3
  • 3
  • +1
14 Comments
 
LVL 53

Expert Comment

by:COBOLdinosaur
ID: 12074680
When you want to do something that loose, you are not going do it without playing with the security setting.

Is there some reason why you have rejected just using a conventional form to submit the data?

This kind a stuff is an invitation to security breaches, especially when you are going send data a as binary stream hidden in XML.

Cd&
0
 

Author Comment

by:mikevandike
ID: 12092402
I am in the process of re-developing a website and am currently working on the admin section for updating the database. I have decided to use some new technologies for doing this as a kind of learning expierence for me. You can check out a prototype at.

http://www.aor.ie/admin/prototype

I am using a servlet to generate XML for the client.

http://www.aor.ie/aor/servlet/XMLData
to get a list of all property Id's.

http://www.aor.ie/aor/servlet/XMLDa...D=1070634500828
to get all Info on an individual property.

My aim really is to try and give the feel of a native application.(page not reloading ect) I have done this kind of thing before and used hidden frames to post the data to the server and callback javascripts. But as I said before It is more of a learning thing for myself than it is necessary. I also am trying to use XML as much as possible because I am interested in its potential usability.

I understand fully the security ricks involved in accessing files on the client side but you would think that once a user selects a file with the File input control you could access it through the code. Why is this not possible.

John.
0
 

Author Comment

by:mikevandike
ID: 12092411
Also it there a cross browser version of the Microsoft.XMLDOM object.
0
 
LVL 13

Accepted Solution

by:
davidlars99 earned 134 total points
ID: 12092445
sure there is.

var xmlhttp;
 try{
        xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
 }catch(e){
  try{
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
  }catch(E){
        xmlhttp = false;
  }
 }

if(!xmlhttp && typeof XMLHttpRequest!='undefined'){
        xmlhttp = new XMLHttpRequest();
}else{
  xmlhttp = false;
}
0
 
LVL 13

Expert Comment

by:davidlars99
ID: 12092461
in the part where you create XML nodes using XMLDOM, use simple Javascript

var xmlNodes='<node1><node2>some data will go here...</node2></node1>';

and send then via xmlhttp
0
 
LVL 53

Assisted Solution

by:COBOLdinosaur
COBOLdinosaur earned 133 total points
ID: 12092710
Mozilla based browsers use RDF instead of XMLDOM, but they are behind in XML in general because they won't compromise on security issues:

http://www.mozilla.org/rdf/doc/

To do a POST in Moz browsers the code is:

netscape.security.PrivilegeManager.enablePrivilege("UniversalBrowserRead");
var httpRequest = new XMLHttpRequest();
httpRequest.open("POST", url, false);
httpRequest.send(xmlDocument);


However I suspect that will also run into security issues. It is not HTTP that is the but browser security, you probably need to use a custom JAVA browser or write an activex control to be as intrusive as that is. If I need something like that I would give it to a VB or C++ developer to develop the client code.  It's just not something I would do with  web page.

Cd&
0
 
LVL 4

Assisted Solution

by:Michael Krumpe
Michael Krumpe earned 133 total points
ID: 12097358
To keep things simple... dont use the WinXMLHTTP object at all. I made that mistake myself when trying to do very simular things that you are doing, and that object complicates issues if you are not actually trying to use its extended DOM functionality.

So use what?

The XMLHTTP object itself uses a smaller object on the server called the WinHTTP object. It will do what you are looking for.
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winhttp/http/winhttprequest.asp?frame=true

Now, in your case, what you will want to do is use the post function, just as if you were posting a form and set the mime type of the post to multipart, just as if you were making a form to do the same.

That is your conceptual solution and will work. Your exact symantecs on how you plan to code it, I suggest view the WinHTTP examples in the link above and put the post fields together per your needs. Done.

Keep it simple,

Michael

0
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
LVL 4

Expert Comment

by:Michael Krumpe
ID: 12097367
If you use the WinHTTP object like I am suggesting, you will not need to do any client side scripting as others have suggested and the file will go out from the server itself.

Michael
0
 

Author Comment

by:mikevandike
ID: 12118625
mkrumpe I dont get what you are saying. It is not a microsoft server and the sever side language I am using is Java. All I want to know is once a user selects a file with the normal file input control. Is their any way on the client side I can access that files data without changing the security settings or is the only way to send that file to the server by clicking on a submit button or using form.submit();
0
 
LVL 13

Expert Comment

by:davidlars99
ID: 12165001
mkrumpe, can you please provide a little example of what you are describing..? It is very interestning... :)
0
 
LVL 4

Expert Comment

by:Michael Krumpe
ID: 12165008
The only example I could show you would be in .asp. But the winhttp object should be able to be used by any language.

Is that ok?
0
 
LVL 13

Expert Comment

by:davidlars99
ID: 12165026
aaah, I think I got what you saying... post data by multipart and then send xml down to the browser using xmlhttprequest right?
0
 
LVL 13

Expert Comment

by:davidlars99
ID: 12165043
here, I opened up another topic
> http://experts-exchange.com/Web/Q_21147124.html
0
 
LVL 13

Expert Comment

by:davidlars99
ID: 12165048
mkrumpe, post your suggestion in there..
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

I will show you how to create a ASP.NET Captcha control without using any HTTP HANDELRS or what so ever. you can easily plug it into your web pages. For Example a = 2 + 3 (where 2 and 3 are 2 random numbers) Session("Answer") = 5 then we…
I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
The viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now