首页 > 解决方案 > 通过带有自定义标头的 shdocvw.dll 下载文件

问题描述

我需要通过 vba 应用程序在 msaccess 中下载一个非常大的文件。

使用对象 MSXML2.ServerXMLHTTP.6.0 和 WinHttp.WinHttpRequest.5.1 会导致错误,指出没有足够的可用存储空间来完成此操作。因此,我使用了来自 shdocvw.dll 的 DoFileDownload 方法。我想要做的是将额外的标头(API 密钥)传递给函数发送的请求。

这大致是我想要做的。

Private Declare Function DoFileDownload Lib "shdocvw.dll" _
  (ByVal lpszFile As String) As Long


Public Sub Download()
    sDownloadFile = StrConv(<link_to_download>, vbUnicode)
    'set a header before calling DoFileDownload
    Call DoFileDownload(sDownloadFile)
End Sub

我该如何解决这个问题?

标签: vbams-access

解决方案


一次下载整个文件的 WebRequest 会存储整个数据作为响应。

虽然有分块响应的选项,但使用Wget是更少的编码,而是更多的选项。

Private Sub DownloadFileWget()
Const PathToWget As String = "" 'if wget is not in path use  "Path\To\Wget"
Dim LinkToFile As String
Dim SavePath As String

With CreateObject("WScript.Shell")
    LinkToFile = "http://download.windowsupdate.com/microsoftupdate/v6/wsusscan/wsusscn2.cab" 'huge file > 500MB
    SavePath = "C:\doc" 'folder to save download
    .CurrentDirectory = SavePath
    .Run Chr(34) & PathToWget & "wget.exe" & Chr(34) & " --header='name: value' " & Chr(34) & LinkToFile & Chr(34) & " -N", 1, True
     ' -N: Continue download only if the local version is outdated.
End With
End Sub

推荐阅读