perfectxml.com
 Basic Search  Advanced Search   
Topics Resources Free Library Software XML News About Us
home » focus » msxml » ask a question » past questions & answers Monday, 13 August 2007
 
NEWS
MSXML 4.0 SP2 now available!

 
MSXML
Basics
DOM
SAX
XPath
XSLT
Schemas
SOM
HTTP Access
.NET
Data Islands
Ask a Question
   Past Q&As
C++ Samples
DLL/Version Info
Reference Guide
Books
KB Articles
   HOW TO
   SAMPLE
   INFO
   BUG/PRB
   FIX
   Misc.
MSXML Tips
   August 2002
   September 2002
MSXML Tools

Microsoft XML Core Services


Go back to list of previously asked questions and answers

Question: I would like POST a binary data file from one server to the other. The way I am thinking is to Base64 encode the data using MSXML DOM and then POST using XMLHTTP to the remote server. Then on the remote server load the XML, decode base64 to binary and save it as a local file on the remote server. Can you please provide an ASP example of doing this?
Asked By: Guest
Viewed: 25607
Answer: You can use ADODB Stream to read the local file, MSXML DOM to Base64 encode it and ServerXMLHTTP to POST the created XML document to the remote server. (XMLHTTP is not designed to be used on the server side; use ServerXMLHTTP instead). On the remote server, the ASP page can load the received XML document (using the Request stream), load it into DOMDocument, decode it and use ADODB Stream to save the binary data into a file.

Here is an example of reading a (binary) file on server, posting it on another remote server and saving the binary data there.

PostData.asp: This page accepts a parameter which is the name of the file to be POSTed to the remote server.

<%
Option Explicit
Dim objStream
Dim objXMLDoc 
Dim objDocElem
Dim strFileName
Dim objSXH
	
strFileName = Request("f")

Set objXMLDoc = Server.CreateObject("MSXML2.DOMDocument.4.0")

'The root node itslef will contain the base64 encoded data
objXMLDoc.loadXML "<Base64Data />"

'Read the file into the stream
Set objStream = Server.CreateObject("ADODB.Stream")
objStream.Type = 1
objStream.Open
objStream.LoadFromFile Server.MapPath("Files/" & strFileName)

'Do base64 encoding
Set objDocElem = objXMLDoc.documentElement
objDocElem.dataType = "bin.base64"
objDocElem.nodeTypedValue = objStream.Read
    
'Add FileName Attribute
objDocElem.setAttribute "FileName", strFileName

'POST the document on the remote server
Set objSXH = Server.CreateObject("MSXML2.ServerXMLHTTP.4.0")
objSXH.open "POST", "http://SomeRemoteServer/SaveData.asp", False
objSXH.setRequestHeader "Content-Type", "text/xml"
objSXH.send objXMLDoc.xml 

'POST Succeeded
If objSXH.status = 200 Then
	Response.Write "The POST succeeded. " 
Else
	Response.Write "The POST failed: (" & objSXH.status & ") " & _
		objSXH.statusText
End If
%>

The above code uses ADODB Stream to load a binary file, and MSXML DOM to base64 encode it and then it uses ServerXMLHTTP to post the created XML document to the remote server. The above code generates XML document which looks similar to:


<Base64Data 
            xmlns:dt="urn:schemas-microsoft-com:datatypes" 
            dt:dt="bin.base64" 
            FileName="purpledot.gif">

                       R0lGODlhBQABAIAAAHkqhf///ywAAAAABQABAAACA0xgBQA7

</Base64Data>


SaveData.asp:
The following ASP page on the remote server loads the POSTed XML document using the Request stream, decodes the base64 data to binary and uses ADODB stream to save the binary data to the local file. The FileName attribute's value is used to name the local file.
<%
	Option Explicit
    Dim objXMLDoc 
    Dim objStream
    Dim strFileName

	'Create MSXML DOMDocument Object
    Set objXMLDoc = Server.CreateObject("MSXML2.DOMDocument.4.0")
    objXMLDoc.async = False
    objXMLDoc.validateOnParse = False
    
    'And load it from the request stream
    If objXMLDoc.load (Request) Then
    
		'Use ADO stream to save the binary data
		Set objStream = Server.CreateObject("ADODB.Stream")
		objStream.Type = 1
		objStream.Open

		'The nodeTypedValue automagically converts Base64 data to binary data
		'Write that binary data to the stream
		objStream.Write objXMLDoc.selectSingleNode("/Base64Data").nodeTypedValue 
		
		'Get the FileName attribute's value
		strFileName = objXMLDoc.selectSingleNode("/Base64Data/@FileName").nodeTypedValue 
		
		'Save the binary stream to the file
		objStream.SaveToFile "D:\Temp\RecdFiles\" & strFileName
		
		objStream.Close()
		
		Set objStream = Nothing
		
    Else
		'Failed to load the document
    End If
	
%>

Related Links


Go back to list of previously asked questions and answers
  Contact Us | E-mail Us | Site Guide | About PerfectXML | Advertise ©2004 perfectxml.com. All rights reserved. | Privacy