Getting Remote Files With ASP - PerformUpdate() Explained
(Page 4 of 5 )
Within the PerformUpdate() subroutine, we send the request to the server, dumb the results into one big string variable, read that into an XML document, and parse the updates and messages.
srvXmlHttp.open "GET", sServerURL & “?” & sGETData, false
srvXmlHttp.send()
sResponse = srvXmlHttp.responseText
Set srvXmlHttp = Nothing
If ( xmlDOMDocument.loadXML(sResponse) ) Then
This if/then will fail in cases where the sServerURL is either unreachable or if the connection to the server times-out. The On Error Resume Next allows us to throw in an "else" statement, which is basically a way to hotwire ASP to allow us a try/catch block.
The first things we parse are the messages or text data passed from the server to the client. In our example, we check for three variables: MESSAGE, VERSION, and CODE. You can check for whatever you like. Just make sure that there are corresponding variables sending data in the server script –- you’ll see these three in the server code below. As I mentioned before, the config file that will store the current version and key, can be updated with the new version value once the update successfully completes.
set objXMLList = xmlDOMDocument.getElementsByTagName("MESSAGE")
If ( objXMLList.Length > 0 ) Then
iMessage = Trim(objXMLList.item(0).text)
End If
set objXMLList = xmlDOMDocument.getElementsByTagName("VERSION")
If ( objXMLList.Length > 0 ) Then
sVersion = Trim(objXMLList.item(0).text)
End If
set objXMLList = xmlDOMDocument.getElementsByTagName("CODE")
If ( objXMLList.Length > 0 ) Then
sCode = Trim(objXMLList.item(0).text)
End If
After we get the text data, we begin getting the files. We loop through the XML looking for PATH nodes. For each PATH node, there is a corresponding TRANSFERFILE node. The PATH node contains path information that can be used to tell the client where to store the file. In our example, it simply holds the file name. If you like, you can add the full or relative path to retain the folder structure. The TRANSFERFILE node is the file itself.
You’ll notice that within the loop, we do a check for a PATH with the word “UpdateScripts” in it (see the code below). This is how we are flagging our update script file. This is a file named UpdateScripts.asp on the server. Within that file is ASP code that will run on the client. The ExecuteGlobal() runs the actual code from this file. If the code that is sent from the server fails, an error is generated and trapped. If the file is not an update script, the system calls the WriteFile() subroutine with the name and type of the file to transfer.
An error in the process will set the iMessage variable as E_UPDATESCRIPTFAILURE and exit the For loop. There you would want to alert the administrator that the update failed.
If ( iMessage <> "" ) Then
Set fsoFileObject = CreateObject("Scripting.FileSystemObject")
Set objXMLList = xmlDOMDocument.getElementsByTagName("MALL23")
For iX = 0 to (objXMLList.length - 1)
sPath = ""
For iY = 0 to ((objXMLList.item(iX).childNodes.length)-1)
If ( objXMLList.item(iX).childNodes(iY).nodeName = "PATH" ) Then
sPath = objXMLList.item(iX).childNodes(iY).text
End If
If ( sPath <> "" ) And
( objXMLList.item(iX).childNodes(iY).nodeName = " TRANSFERFILE" ) Then
If ( InStr(sPath, "UpdateScripts") > 0 ) Then
If ( Trim(objXMLList.item(iX).childNodes(iY).text) <> "" ) Then
Response.Write(vbCrLf & _
"<script language=""JavaScript"">" & _
VbCrlf & VbCrlf & _ "document.UpdateForm.UpdateText.value = " & _ "'Beginning Update Script...';" & _
VbCrlf & VbCrlf & _
"</script>")
ExecuteGlobal(objXMLList.item(iX).childNodes(iY).text)
If ( Err.Number <> 0 ) Then
iMessage = E_UPDATESCRIPTFAILURE
Exit For
Else
Response.Write(vbCrLf & _
"<script language=""JavaScript"">" & _
VbCrlf & VbCrlf & _ "document.UpdateForm.UpdateText.
value = " & _ "'Finished Update Script';" & _
VbCrlf & VbCrlf & _
"</script>")
End If
End If
Else
If ( InStr(sPath, ".asp") <= 0 )
And ( InStr(sPath, ".txt") <= 0 ) Then
Call WriteFile(sPath,
bjXMLList.item(iX).childNodes(iY).nodeTypedValue,
C_BINARY)
Else
Call WriteFile(sPath,
objXMLList.item(iX).childNodes(iY).text,
C_TEXT)
End If
End If
End If
Next
If ( iMessage = E_UPDATESCRIPTFAILURE ) Then
‘ alert admin here
Exit For
End If
Next
End If
Set xmlDOMDocument = Nothing
Set fsoFileObject = Nothing
End If
Next: WriteFile() Explained >>
More ASP Articles
More By Justin Cook