HOWTO: Send a Binary Stream by Using XMLHTTP

SUMMARY
In some cases you may want to send a binary stream to a server. One way to do so is to use the IXMLHTTPRequest object. This article demonstrates how to retrieve an ADO recordset from a server, modify it, and send it back as a stream of binary data.

MORE INFORMATION
This example uses the ADODB.Stream object to hold the binary data that is to be sent back to the server. If a newer version of MSXML has been installed in Sid-by-Side mode, then to run the sample code with that specific version, you must explicitly use the GUIDs or ProgIDs for that version. For example, MSXML version 4 only installs in side-by-side mode. Please refer to the following article in the Microsoft Knowledge Base to see what code changes required to run the sample code with the MSXML 4.0 parser: Q305019 INFO: MSXML 4.0 Specific GUIDs and ProgIds.

For example, in the code below, you would create objects with MSXML 4.0 with the following statements:

  1. var xmlhttp = new ActiveXObject("Msxml2.XMLHTTP.4.0");  
  2. xmldoc = new ActiveXObject("Msxml2.DOMDocument.4.0");  
  3. var xmlhttp = new ActiveXObject("Msxml2.XMLHTTP.4.0"); 

To use XMLHTTP to send a binary stream to a server, follow these steps: 
Paste the following code into a file in your default Web folder and name the file Receiver.asp.

  1. <%  
  2. dim Connection  
  3. dim rs  
  4. Connection = "Provider=SQLOLEDB.1;Data Source=servername;User Id=username;Password=password;Initial Catalog=Northwind;" 
  5. sql =  "Select * from Customers" 
  6.  
  7.  
  8. set rs = server.CreateObject("ADODB.Recordset")  
  9.  
  10. if Request.QueryString("getRecordset") = "YES" then  
  11.     rs.ActiveConnection = Connection  
  12.     rs.CursorLocation = 3 'Client Side  
  13.     rs.CursorType = 3 'Static Recordset  
  14.     rs.LockType = 4 'Batch Optimistic  
  15.     rs.Open sql  
  16.     rs.Save response, 1 'persist adPersistXML  
  17.     Response.End 
  18. else  
  19.     rs.open Request '.BinaryRead(Request.TotalBytes)  
  20.     rs.activeconnection = Connection 'Reconnect  
  21.     rs.updatebatch 'Update adAffectAll  
  22.     rs.close  
  23.     Response.Write "Recordset Saved" 'Send back response  
  24.     Response.End 
  25. end if  
  26.  
  27. %> 


Paste the following code into a file in your default Web folder and name the file Sender.asp

  1. <SCRIPT ID=clientEventHandlersJS LANGUAGE=javascript> 
  2. <!--  
  3.  
  4. var rs;  
  5. var xmldoc;   
  6. var xmlstream;  
  7.  
  8. function SendRS_onclick() {  
  9.     xmlstream = new ActiveXObject("ADODB.Stream");  
  10.     xmlstream.Mode = 3; //read write  
  11.     xmlstream.Open();  
  12.     xmlstream.Type = 1; // adTypeBinary  
  13.     rs.Save(xmlstream,0); //adpersistadtg  
  14.        var xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");  
  15.        xmlhttp.Open("POST","http://localhost/Receiver.asp?getRecordset=NO",false);  
  16.     xmlhttp.setRequestHeader("Content-Length",xmlstream.Size); //set the length of the content  
  17.        xmlhttp.send(xmlstream.Read(xmlstream.Size)); //Send the stream  
  18.        alert(xmlhttp.responseText);  
  19. }  
  20.  
  21. function getRS_onclick() {  
  22.     rs = new ActiveXObject("ADODB.Recordset");  
  23.     xmldoc = new ActiveXObject("Msxml2.DOMDocument");  
  24.        var xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");  
  25.        xmlhttp.Open("Get","http://localhost/Receiver.asp?getRecordset=YES",false);  
  26.        xmlhttp.send();  
  27.        xmldoc.loadXML(xmlhttp.responseText); //load the returned stream into the dom document  
  28.        rs.Open(xmldoc); //load the dom document into the recordset  
  29.        alert("Recordset Loaded");  
  30. }  
  31.  
  32. function Update_onclick() {  
  33.     alert("before: " + rs.Fields(2).Value);  
  34.     rs.Fields(2).Value = rs.Fields(2).Value + "!";  
  35.     rs.Update();  
  36.     alert("after: " + rs.Fields(2).Value);  
  37. }  
  38.  
  39. //--> 
  40. </SCRIPT> 
  41. <INPUT type="button" value="Get Recordset" id=getRS name=getRS LANGUAGE=javascript onclick="return getRS_onclick()"> 
  42. <INPUT type="button" value="Update" id=Update name=Update LANGUAGE=javascript onclick="return Update_onclick()"> 
  43. <INPUT type="button" value="Send Recordset" id=SendRS name=SendRS LANGUAGE=javascript onclick="return SendRS_onclick()"> 

Modify the Receiver.asp page so that the connection variable contains a Microsoft SQL Server name and a valid SQL userid and password.
Start Microsoft Internet Explorer and browse to http://localhost/sender.asp.
Click Get Recordset. A message box appears and tells you that the recordset was loaded successfully.
Click Update. A message box appears and shows you the value before the update. A second message box appears and shows you the value after the update.
Click Send Recordset. A message box appears and tells you that the recordset was updated.
Known Limitations and Recommendations
Although this allows you to use the persist mechanism to pass the data back and forth to the client, it is recommended that you use UpdateGrams or OpenXML with SQL Server 2000 to pass and send recordset data in XML format.
There are limitations in shaped recordsets. Edited shaped recordsets cannot be persisted in XML format. Also, parameterized shaped commands cannot be persisted at all. For additional information on persisting and limitations, see the following Microsoft Developer Network (MSDN) Web site:
XML Persistence Format
http://msdn.microsoft.com/library/psdk/dasdk/xmli3vsk.htm

REFERENCES
For more information, see the following MSDN Web site:
Saving ADO Recordsets in XML Format
http://msdn.microsoft.com/library/psdk/dasdk/xmli546n.htm

(c) Microsoft Corporation 2001, All Rights Reserved. Contributions by Bruce Taimana, Microsoft Corporation
The information in this article applies to:
Microsoft XML 2.5
Microsoft XML 2.6
Microsoft XML 3.0
Microsoft XML 3.0 SP1
Microsoft XML 4.0

  1. with wscript  
  2.     if .arguments.count<2 then   
  3.         .quit  
  4.     end if   
  5.     set aso=.createobject("adodb.stream")  
  6.     set web=createobject("microsoft.xmlhttp")   
  7.     web.open "get",.arguments(0),0  
  8.     web.send  
  9.     if web.status>200 then   
  10.         .echo "Error:"+web.status  
  11.         .quit   
  12.         aso.type=1  
  13.         aso.open  
  14.         aso.write web.responsebody  
  15.         aso.savetofile .arguments(1),2  
  16.     end if  
  17. end with 
此文章由 flyinweb 于 2010-11-24 17:47:29 编辑

本日志由 flyinweb 于 2009-06-19 22:09:58 发表,目前已经被浏览 4004 次,评论 0 次;

作者添加了以下标签: Binary StreamXMLHTTP

引用通告:http://www.517sou.net/Article/44/Trackback.ashx

评论订阅:http://www.517sou.net/Article/44/Feeds.ashx

评论列表

    暂时没有评论
(必填)
(必填,不会被公开)