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

查看:20
本文介绍了下载文件 - 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 有一篇使用 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.

来自文章:

URLDownloadToFile API 可用在所有版本的 Windows 上操作系统(Win3 除外,WinNT3.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

仅供参考 - 在 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天全站免登陆