简体   繁体   English

下载文件 - VB6

[英]Download File - VB6

有谁知道如何下载文件(不打开网页),并将其保存到 Visual Basic 6.0 中的目录?

If you want to do it with code only (no Internet Transfer Control), VBNet.mvps.org has a really good how-to article that uses the URLDownloadToFile API call.如果您只想使用代码(无 Internet 传输控制)来完成此操作, VBNet.mvps.org有一篇使用 URLDownloadToFile API 调用的非常好的操作方法文章。

From the article:从文章:

The URLDownloadToFile API is available on all versions of the Windows operating system (except Win3, WinNT3.x). URLDownloadToFile API 适用于所有版本的 Windows 操作系统(Win3、WinNT3.x 除外)。 By passing the remote file name and the local file path and name, the API downloads the bits of the specified file saving them as the target name.通过传递远程文件名和本地文件路径和名称,API 下载指定文件的位,将它们保存为目标名称。 The function works with all file types - plain text, images, html, mpg, wav and zip files etc. without modification to the routine or concern for the file being downloaded, nor is there any apparent size restriction or limitation.该功能适用​​于所有文件类型 - 纯文本、图像、html、mpg、wav 和 zip 文件等,无需修改例程或关注正在下载的文件,也没有任何明显的大小限制或限制。

Private Declare Function URLDownloadToFile Lib "urlmon" _
   Alias "URLDownloadToFileA" _
  (ByVal pCaller As Long, _
   ByVal szURL As String, _
   ByVal szFileName As String, _
   ByVal dwReserved As Long, _
   ByVal lpfnCB As Long) As Long

Private Const ERROR_SUCCESS As Long = 0
Private Const BINDF_GETNEWESTVERSION As Long = &H10
Private Const INTERNET_FLAG_RELOAD As Long = &H80000000

Public Function DownloadFile(sSourceUrl As String, _
                             sLocalFile As String) As Boolean

  //'Download the file. BINDF_GETNEWESTVERSION forces 
  //'the API to download from the specified source. 
  //'Passing 0& as dwReserved causes the locally-cached 
  //'copy to be downloaded, if available. If the API 
  //'returns ERROR_SUCCESS (0), DownloadFile returns True.
   DownloadFile = URLDownloadToFile(0&, _
                                    sSourceUrl, _
                                    sLocalFile, _
                                    BINDF_GETNEWESTVERSION, _
                                    0&) = ERROR_SUCCESS

End Function

FYI - in testing on Windows 7, it would only return the cached version, so I had to use the extra function mentioned in the article to clear it first (and that worked).仅供参考 - 在 Windows 7 上的测试中,它只会返回缓存的版本,所以我不得不使用文章中提到的额外功能来首先清除它(并且有效)。

Private Declare Function DeleteUrlCacheEntry Lib "Wininet.dll" _
   Alias "DeleteUrlCacheEntryA" _
  (ByVal lpszUrlName As String) As Long

Then just call the above function with the destination URL first, to clear the cache.然后只需先使用目标 URL 调用上述函数,即可清除缓存。

You don't need API calls, you don't need the Internet Transfer control.您不需要 API 调用,也不需要 Internet Transfer 控件。 Just do it the easy way, using native VB6 code.使用本机 VB6 代码,以简单的方式完成。 Here's an excellent article by Karl Peterson with sample code.这是Karl Peterson的一篇优秀文章,其中包含示例代码。

Try this尝试这个

Sub DownloadFile(url, path)

   Dim objReq
   Dim objStream

   Set objReq = CreateObject("MSXML2.XMLHTTP")
   objReq.Open "GET", url, False
   objReq.send

   If objReq.Status = 200 Then
       Set objStream = CreateObject("ADODB.Stream")
       objStream.Open
       objStream.Type = 1

       objStream.Write objReq.ResponseBody
       objStream.Position = 0

       objStream.SaveToFile path, 2
       objStream.Close
       Set objStream = Nothing
   End If

   Set objReq = Nothing

End Sub

我建议使用Internet Transfer Control

You need to use the Internet Transfer control, see http://www.vb-helper.com/howto_get_file_from_web.html for a sample.您需要使用 Internet Transfer 控件,请参阅http://www.vb-helper.com/howto_get_file_from_web.html以获取示例。 If you need to specify credentials, check out http://support.microsoft.com/kb/173264 as well.如果您需要指定凭据,也请查看http://support.microsoft.com/kb/173264

I don't like the Internet Transfer Control because it is synchronous.我不喜欢 Internet Transfer Control,因为它是同步的。 Once you start a download your application is unresponsive until the file is downloaded or an error is thrown.开始下载后,您的应用程序将无响应,直到下载文件或引发错误。 There are plenty of good examples of using the WININET DLL to write asynchronous methods.有很多使用WININET DLL 编写异步方法的好例子。 It is not trivial, but it is also very do-able.这不是微不足道的,但它也是非常可行的。 Here is an example from stackoverflow. 是来自 stackoverflow 的示例。

Try This:尝试这个:

My.Computer.Network.DownloadFile (*File to download*, *What to save it as*)

You must give it a filename in the what to save it as :您必须在将其另存为内容中为其指定一个文件名:

Example:例子:

 My.Computer.Network.DownloadFile _
("http://www.cohowinery.com/downloads/WineList.txt", _
"C:\Documents and Settings\All Users\Documents\WineList.txt")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM