下载文件 - VB6 [英] Download File - VB6

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

问题描述

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

Does anyone know how to download a file (without opening a webpage), and save it to a directory in Visual Basic 6.0?

推荐答案

如果您只想使用代码(无Internet传输控制), VBNet.mvps.org 有一个非常好的how-to文章,使用URLDownloadToFile API调用。

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.

从文章:


所有版本的Windows
操作系统(除Win3,
WinNT3之外),可以使用URLDownloadToFile API
。 X)。通过传递远程文件
名称和本地文件路径和名称,
API下载
指定文件的位将其保存为
目标名称。该功能适用​​于
所有文件类型 - 纯文本,图像,
html,mpg,wav和zip文件等。
不修改例程或
关心的文件是下载,
也没有任何明显的大小
限制或限制。

The URLDownloadToFile API is available on all versions of the Windows operating system (except 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. 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.



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 - 在Windows 7测试中,只会返回缓存版本,所以我有使用文章中提到的额外的功能首先清除它(并且有效)。

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

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

然后,首先使用目标URL调用上述函数,以清除缓存。

Then just call the above function with the destination URL first, to clear the cache.

这篇关于下载文件 - VB6的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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