Getting Remote Files With ASP Continued - ProcessFile() Explained
(Page 4 of 5 )
The ProcessFile() subroutine appends both the PATH and TRANSFERFILE (the encoded file itself) data to the XML message.
When adding the TRANSFERFILE node, .txt and .asp files are simply read as text files using FSO and appended to the XML as basic string nodes. If you have any other files that should be read as text (html files, js files, css files, vbs files, and so on), simply add their extension to the ‘if ( InStr(sPath,’ line. Binary files, however, need to be opened using ADO’s Stream object. Then the file is encoded as a Base64 text string, specified as such in the XML node, then appended to the document. This is accomplished with a free xmlDOMDocument object that Microsoft provides. Thanks Microsoft!!!
If you uncomment the following two debug lines client script (last article):
'Response.Write sResponse
'Response.End
the browser will print out the Base64 encoded text for you when you run the client script in your browser. You’ll notice it looks just like a big square of numbers and letters. The client receives this, and based on the filename in the PATH node, it knows if it’s a normal text file or a binary file. If it’s binary, the client reads the XML as Base64 and uses the ADO object to render the encoded file back into its raw binary form, as we saw in the first article.
When the binary file is read into memory using the ADO stream object, it is at that point that the dataType property is set to "bin.base64".
Here’s the code for the subroutine:
Sub ProcessFile(sPath)
Dim oRoot, oElement, fFile, tsTextStream
Const ForReading = 1, TristateFalse = 0
Set oRoot = xmlDOMDocument.documentElement
Set oElement = xmlDOMDocument.createElement("PATH")
oElement.nodeTypedValue = sPath
oRoot.appendChild oElement
'Here comes the Binary data part
Set oElement = xmlDOMDocument.createElement("TRANSFERFILE")
If ( InStr(sPath, ".asp") <= 0 ) And ( InStr(sPath, ".txt") <= 0 ) Then
objStream.Type = 1 'adTypeBinary
objStream.Open
objStream.LoadFromFile(sPath)
oElement.dataType = "bin.base64"
oElement.nodeTypedValue = objStream.Read
objStream.Close
Else
‘ just read it as a plain text file
Set fFile = fsoFileObject.GetFile(sPath)
Set tsTextStream = fFile.OpenAsTextStream(ForReading, TristateFalse)
oElement.nodeTypedValue = tsTextStream.ReadAll
tsTextStream.Close
End If
‘ add it to the XML document
oRoot.appendChild oElement
End Sub
Next: Conclusion >>
More ASP Code Articles
More By Justin Cook