Getting Remote Files With ASP - WriteFile() Explained
(Page 5 of 5 )
There is nothing fancy about how we tell if a file is binary or text. We just look at the extension. Remember that we passed the file name in the PATH XML node.
Notice that with binary files, we are using the XML nodeTypedValue rather than the Text property. This property returns the data in whatever type it was set as on the server side (see the server code below). Since the server code sets all binary files as “bin.base64”, the client will then read the data in as Base64 encoded. Remember that the XML that is being passed also holds status data, such as the type.
The WriteFile() subroutine uses ADO to write our binary files. We can use normal text-based FSO operations for the .txt and .asp files. Here’s the meat of the routine, doing the actual saving work:
If ( iType = C_BINARY ) Then
Set objBinaryStream = CreateObject("ADODB.Stream")
objBinaryStream.Type = adTypeBinary
objBinaryStream.Open
objBinaryStream.Write vData
objBinaryStream.SaveToFile (sDestPath & sFileName), adSaveCreateOverWrite
Set objBinaryStream = Nothing
Else
Set fileWriteFile = fsoFileObject.OpenTextFile(sDestPath & sFileName, 2, True)
fileWriteFile.Write vData
fileWriteFile.Close
Set fileWriteFile = Nothing
End If
IMPORTANT: Make sure the final destination on the client has WRITE rights granted to the IUSR_computername account or the write will fail. To make this more robust, you can test permissions with an ON ERROR trap. Write a dummy file to the destination path. If an error is generated, you can display a nice message to the client, and send a message back to the server, telling the administrator that the update failed.
Conclusion In this first of two articles, we saw how to connect to a remote server, retrieve text and encoded binary files, and save them locally. In the next article, we will see how to process the request on the server side to actually send the files. Here’s a link to the full working client script, and the two images referenced:
[http://images.devshed.com/af/stories/Getting Remote Files with ASP/Client.zip]
To see this in action, check out Mall23 auto-update feature (it’s pretty impressive!): http://www.mall23.com/store/?vf=14
| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |