使用vbscript下载,某些文件不完整 [英] Downloading with vbscript, some files not complete

查看:56
本文介绍了使用vbscript下载,某些文件不完整的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用以下vbscript来自动从我的网站下载应用程序:

I'm using the following vbscript to automate the download of an app from my site:

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)。

Weirdly, when I run the script, 2/4 files are downloaded fully. One of the other files is only 2 KB (should be about 180kb full) and the other one is 0 KB (200kb full).

我在ftp服务器上仔细检查了,存储的文件已100%完整,并且可以通过浏览器手动下载。

I double checked on the ftp server, the files stored their are 100% complete and downloading them manually via the browser works fine.

为什么我的脚本无法完全下载所有四个文件?

Why would my script fail to fully download all four files?

推荐答案

尝试使用 XMLHTTPRequest 而不是 WinHttpRequest ,并使用 ADO流用于保存(二进制)文件:

Try with an XMLHTTPRequest instead of a WinHttpRequest, and use an ADO stream for saving the (binary) files:

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

请求状态可能会给您有关问题的一些指示。还要检查服务器日志(如果可能)。

The request status may give you some pointers about issues. Also check the server log (if possible).

这篇关于使用vbscript下载,某些文件不完整的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆