我正在使用以下vbscript自动从我的网站下载应用程序:
HTTPDownload "bleh.com/hello.exe", "C:\"
HTTPDownload "bleh.com/hello1.dll", "C:\"
HTTPDownload "bleh.com/hello2.dll", "C:\"
HTTPDownload "bleh.com/hello3.dll", "C:\"
Sub HTTPDownload( myURL, myPath )
Dim i, objFile, objFSO, objHTTP, strFile, strMsg
Const ForReading = 1, ForWriting = 2, ForAppending = 8
Set objFSO = CreateObject( "Scripting.FileSystemObject" )
If objFSO.FolderExists( myPath ) Then
strFile = objFSO.BuildPath( myPath, Mid( myURL, InStrRev( myURL, "/" ) + 1 ) )
ElseIf objFSO.FolderExists( Left( myPath, InStrRev( myPath, "\" ) - 1 ) ) Then
strFile = myPath
Else
Exit Sub
End If
Set objFile = objFSO.OpenTextFile( strFile, ForWriting, True )
Set objHTTP = CreateObject( "WinHttp.WinHttpRequest.5.1" )
objHTTP.Open "GET", myURL, False
objHTTP.Send
For i = 1 To LenB( objHTTP.ResponseBody )
objFile.Write Chr( AscB( MidB( objHTTP.ResponseBody, i, 1 ) ) )
Next
objFile.Close( )
End Sub
奇怪的是,当我运行脚本时,会完全下载2/4个文件。其中一个文件只有2 KB(应该大约180kb满),另一个是0 KB(200kb满)。
我在ftp服务器上仔细检查,存储的文件是100%完成的,并通过浏览器手动下载它们工作正常。
为什么我的脚本无法完全下载所有四个文件?
答案 0 :(得分:0)
尝试使用XMLHTTPRequest
代替WinHttpRequest
,并使用ADO stream保存(二进制)文件:
Sub HTTPDownload(url, path)
Set req = CreateObject("Msxml2.XMLHTTP.6.0")
req.open "GET", url, False
req.send
If req.Status = 200 Then
Set stream = CreateObject("ADODB.Stream")
stream.Open
stream.Type = 1 'binary
stream.Write req.responseBody
stream.SaveToFile path
stream.Close
Else
WScript.Echo req.status & " " & req.statusText
End If
End Sub
请求状态可能会为您提供有关问题的一些指示。另请检查服务器日志(如果可能)。