This post contains attachments v20020411055942.zip 
Summary
Sometime it's useful to have a quick solution to upload small files to your server, especially if you are working in an Intranet ambient.
Use XML and XMLHTTP Documents to Upload Small Files
Sometime it's useful to have a quick solution to upload small files to your server, especially if you are working in an Intranet ambient.
In this short article I'm going to show you a quick and dirty solution to upload small files without using server side components, or HTML frame parsing. All you need is a simple VBScript (or the corresponding JScript or equivalent) script on the client side and an ASP file on the server site. Clearly, the method I am going to explain, is not a secure one: unauthorized persons can upload file to your server if they know the location and the name of the ASP file on the server. So it's a good advise to not reveal the information about the ASP file I'll refer to as XMLHTTPUploader.asp. This little tool is also a good didactical example for XML HTTP data transmission.
On the client side
I'll start with the XMLHTTPUploader.vbs script on the client side, more precisely with the hart of this script, the function
Function XMLHTTPUploader(strLocalSourceFile, strWebServer).
This function accepts two parameters:
- strLocalSourceFile - the path of your local file to upload
- strWebServer - the web address of your server
and it returns the string OK in case the upload was successful and an error description string otherwise.
First of all we localize the ASP file on the server
' the ASP ulpoader file on the server strHTTPUploaderASPFile = strWebServer & /XMLHTTPUploader.asp
You can put the ASP file in whatever directory you want, but make sure you have write permission in the corresponding directory.
Using the FileSystemObject object we open our local file for reading and we read the file character-wise into the variable strFileData separating the data by a comma :
' read the source file character-wise Do Until objSourceFile.AtEndOfStream
strFileData = strFileData & Asc(objSourceFile.Read(1)) & ,
Loop
To transmit the data via the HTTP protocol we store it in a XML document objXMLDOM. The XML document is as simple as possible. It contains a root element (XMLHTTPUPLOADER), a child element (FILEINFO) having one attribute filename, whose value is the local file name (not the full path). To the FILEINFO element we append the file data in a CDATAsection:
' set the root node (XMLHTTPUPLOADER) Set objRootNode = objXMLDOM.createElement(XMLHTTPUPLOADER) ' append the root node to the XML document objXMLDOM.appendChild objRootNode ' set the file node Set objFileNode = objXMLDOM.createElement(FILEINFO) ' append the FILEINFO file node as a child node of the root node objRootNode.appendChild objFileNode ' set file node path attribute objFileNode.setAttribute filename, strFileName ' create an CDATASection object Set objFileData = objXMLDOM.createCDATASection(strFileData) ' append it to the file node objFileNode.appendChild objFileData
To send this XML document to the server we do not need to save it. Instead we create an XMLHTTP document on the client side and using the POST method we address our XMLHTTPUploader.asp ASP file on the server and send the XML document to it for further processing:
Set objXMLHTTP = CreateObject(Microsoft.XMLHTTP) ' open it with the POST method objXMLHTTP.open POST, strHTTPUploaderASPFile, False ' send the XML document objXMLHTTP.send objXMLDOM
We conclude the client side activity by returning the statusText of the created XMLHTTP object.
On the server side
We have the ASP file XMLHTTPUploader.asp to which we posted our client side XML document containing the file data. The ASP script creates the objXMLDocument XML document to load in the client's request, searches the elements by tag name and reads the file name and the file data:
' search the elements by tag name Set objXMLElements = objXMLDocument.getElementsByTagName(FILEINFO) For Each objElement In objXMLElements ' read the attribut filename to finde the location of the ' file on the server ' in the corresponding directory you must have write permission strOnlineFilePath = objElement.getAttribute(filename) Exit For Next strOnlineFilePath=Request.ServerVariables(APPL_PHYSICAL_PATH) & _ strOnlineFilePath ' read the CDATA section of the XMLDOM document strFileData = objElement.firstChild.nodeValue
Now the server is ready to re-create and save the original file :
' create a FileSystemObject object Set objFSO = CreateObject(Scripting.FileSystemObject) ' create a text file to write, overwrite if already exists Set objFile = objFSO.CreateTextFile(strOnlineFilePath, True, False)
' re-create the original file byte by byte bytBinArray = Split(strFileData, ,) For lngIndex=0 To UBound(bytBinArray)-1 objFile.Write Chr(bytBinArray(lngIndex)) Next
|