6

我想从 VB6 应用程序下载多个文件(主要是图像)。目前我正在使用 URLDownloadToFile 但它一次只允许一个文件并且没有进度条。我想下载多个文件并带有进度条。请帮忙。提前致谢。

我现在的代码:

Dim lngRetVal As Long
lngRetVal = URLDownloadToFile(0, URL, LocalFilename, 0, 0)
If lngRetVal = 0 Then DownloadFile = True
4

2 回答 2

4

You want to download the file asynchronously, so that your VB code continues executing while the download happens. There is a little-known way to do this with native VB6, using the AsyncRead method of UserControl and UserDocument objects - no need for API calls.

Here's an excellent explanation and VB6 code for multiple simultaneous downloads, from the renowned VB6 guru Karl Peterson. The AsyncReadProgress event gives you the BytesRead and BytesMax, which will allow you to display a progress bar.

于 2009-02-11T19:31:11.780 回答
2

你希望得到一个 VB 的答案,但这不是微不足道的。

以下大部分来自http://www.experts-exchange.com/Programming/Languages/Visual_Basic/Q_20571958.html

IBindStatusCallback 接口不能直接从 VB 访问。它必须被引入兼容的类型库。

类型库 olelib.tlb 位于: http ://www.domaindlx.com/e_morcillo/scripts/type/default.asp 下载的 zip 文件名为:tl_ole.zip

您还将找到有关如何使用它的示例。不确定您是否会在 IBindStatusCallback 上找到特定示例,但值得一试。

您可以编写自己的函数将数据转换为字符串,这样您就可以完全控制所有内容:

Option Explicit

Public Const INTERNET_OPEN_TYPE_PRECONFIG = 0
Public Const INTERNET_DEFAULT_HTTP_PORT = 80
Public Const INTERNET_SERVICE_HTTP = 3
Public Const INTERNET_FLAG_RELOAD = &H80000000
Public Const HTTP_QUERY_STATUS_CODE = 19
Public Const HTTP_ADDREQ_FLAG_ADD = &H20000000

Public Declare Function InternetOpen Lib "wininet.dll" Alias "InternetOpenA" (ByVal sAgent As String, ByVal lAccessType As Long, ByVal sProxyName As String, ByVal sProxyBypass As String, ByVal lFlags As Long) As Long
Public Declare Function InternetConnect Lib "wininet.dll" Alias "InternetConnectA" (ByVal hInternetSession As Long, ByVal sServerName As String, ByVal nServerPort As Integer, ByVal sUsername As String, ByVal sPassword As String, ByVal lService As Long, ByVal lFlags As Long, ByVal lContext As Long) As Long
Public Declare Function HttpOpenRequest Lib "wininet.dll" Alias "HttpOpenRequestA" (ByVal hHttpSession As Long, ByVal sVerb As String, ByVal sObjectName As String, ByVal sVersion As String, ByVal sReferer As String, ByVal something As Long, ByVal lFlags As Long, ByVal lContext As Long) As Long
Public Declare Function HttpSendRequest Lib "wininet.dll" Alias "HttpSendRequestA" (ByVal hHttpRequest As Long, ByVal sHeaders As String, ByVal lHeadersLength As Long, ByVal sOptional As String, ByVal lOptionalLength As Long) As Long
Public Declare Function HttpQueryInfo Lib "wininet.dll" Alias "HttpQueryInfoA" (ByVal hHttpRequest As Long, ByVal lInfoLevel As Long, ByRef sBuffer As Any, ByRef lBufferLength As Long, ByRef lIndex As Long) As Long
Public Declare Function InternetCloseHandle Lib "wininet.dll" (ByVal hInet As Long) As Integer
Public Declare Function HttpAddRequestHeaders Lib "wininet.dll" Alias "HttpAddRequestHeadersA" (ByVal hHttpRequest As Long, ByVal sHeaders As String, ByVal lHeadersLength As Long, ByVal lModifiers As Long) As Integer
Public Declare Function InternetReadFile Lib "wininet.dll" (ByVal hFile As Long, ByVal sBuffer As String, ByVal lNumberOfBytesToRead As Long, lNumberOfBytesRead As Long) As Integer
Public Declare Function InternetOpenUrl Lib "wininet.dll" Alias "InternetOpenUrlA" (ByVal hInternet&, ByVal lpszUrl As String, ByVal lpszHeaders As String, ByVal dwHeadersLength&, ByVal dwFlags&, ByVal dwContext&) As Long
Public Declare Function InternetQueryDataAvailable Lib "wininet.dll" (ByVal hFile As Long, lpdwNumberOfBytesAvailable As Long, ByVal dwFlags As Long, ByVal dwContext As Long) As Long

Public Function GetHTML(strURL As String) As String
Const BufferSize = 16384
Dim hSession&, hURL&, lRet&, lBytesAvail&
Dim Buffer As String * BufferSize
Dim BufferLen&, sResult$
    hSession = InternetOpen(vbNullString, INTERNET_OPEN_TYPE_PRECONFIG, vbNullString, vbNullString, 0)
    hURL = InternetOpenUrl(hSession, strURL, vbNullString, ByVal 0&, INTERNET_FLAG_RELOAD, ByVal 0&)
    sResult = ""
    Do
        InternetReadFile hURL, Buffer, Len(Buffer), BufferLen
        If BufferLen > 0 Then sResult = sResult & Left(Buffer, BufferLen)
    Loop Until BufferLen = 0
    GetHTML = sResult
    InternetCloseHandle hURL
    InternetCloseHandle hSession
End Function

您将在此处找到用于执行回调方法的其他资源(向下滚动到底部):

http://www.experts-exchange.com/Programming/Languages/.NET/Visual_Basic.NET/Q_21763861.html

http://www.experts-exchange.com/Programming/Languages/.NET/Visual_Basic.NET/Q_21746456.html

但老实说,如果你想更好地控制它,你最好制作自己的下载功能。VB 中的 TCP/IP 内容实际上非常简单。

-亚当

于 2009-02-11T16:55:11.467 回答