File Download via shdocvw.dll with custom headers

人走茶凉 提交于 2020-06-29 06:54:25

问题


I need to download a really large file in msaccess via a vba application.

Using the objects MSXML2.ServerXMLHTTP.6.0 and WinHttp.WinHttpRequest.5.1 result in an error stating that there is not enough storage available to complete this operation. Therefore i resorted in using the DoFileDownload method from shdocvw.dll. What i want to do is pass an extra header (an API key) to the request sent by the function.

Here is roughly what i want to do.

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

How do i approach this problem?


回答1:


A WebRequest downloading a whole file at once stores the whole data in response.

Although there are options to chunk response, using Wget is less coding, but more options.

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


来源:https://stackoverflow.com/questions/56789366/file-download-via-shdocvw-dll-with-custom-headers

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!