wininet如何在下载期间暂停、继续呢?

zhangjiahuashibaichi 2018-01-07 11:56:27
wininet如何在下载期间暂停、继续呢? 也就是能否在
do---loop 中考虑暂停和继续下载

Private Const scUserAgent = "BF"
Private Const INTERNET_OPEN_TYPE_DIRECT = 1
Private Const INTERNET_OPEN_TYPE_PROXY = 3
Private Const INTERNET_FLAG_RELOAD = &H80000000

Private Declare Function InternetOpen Lib "wininet" Alias "InternetOpenA" (ByVal sAgent As String, ByVal lAccessType As Long, ByVal sProxyName As String, ByVal sProxyBypass As String, ByVal lFlags As Long) As Long
Private Declare Function InternetCloseHandle Lib "wininet" (ByVal hInet As Long) As Integer
Private Declare Function InternetReadFile Lib "wininet" (ByVal hFile As Long, ByRef sBuffer As Byte, ByVal lNumBytesToRead As Long, lNumberOfBytesRead As Long) As Integer
Private Declare Function InternetOpenUrl Lib "wininet" Alias "InternetOpenUrlA" (ByVal hInternetSession As Long, ByVal lpszUrl As String, ByVal lpszHeaders As String, ByVal dwHeadersLength As Long, ByVal dwFlags As Long, ByVal dwContext As Long) As Long

Function DownFile(ByVal strURL As String, ByVal strPath As String) As Boolean
On Error GoTo ERR:
Dim hOpen As Long, hFile As Long, sBuffer() As Byte, Ret As Long

hOpen = InternetOpen(scUserAgent, INTERNET_OPEN_TYPE_DIRECT, vbNullString, vbNullString, 0)
If hOpen = 0 Then DownFile = False: Exit Function
hFile = InternetOpenUrl(hOpen, strURL, vbNullString, ByVal 0&, INTERNET_FLAG_RELOAD, ByVal 0&)
If hFile = 0 Then DownFile = False: Exit Function

If Dir(strPath) <> "" Then
If (MsgBox("目标文件存在,是否覆盖?", vbYesNo)) = vbYes Then
Kill strPath
Else
DownFile = False
Exit Function
End If
End If
Open strPath For Binary As #1
ReDim sBuffer(999)
Do
InternetReadFile hFile, sBuffer(0), 1000, Ret

If Ret <> 0 Then
If Ret < 1000 Then ReDim Preserve sBuffer(Ret - 1)
Put #1, , sBuffer
Else
Exit Do
End If
DoEvents
Loop

Close #1

InternetCloseHandle hFile
InternetCloseHandle hOpen

DownFile = True
MsgBox "over"
Exit Function

ERR:
DownFile = False
End Function

Private Sub Command1_Click()
DownFile "http://www.51test.net/show/8619192.html", "c:\8619192.html"
End Sub

Private Sub Command2_Click()
---代码; 暂停下载 或者 继续下载

End Sub
...全文
607 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
赵4老师 2018-01-15
  • 打赏
  • 举报
回复
18 没有更多文件。
赵4老师 2018-01-11
  • 打赏
  • 举报
回复
参考下面? StrConv 函数 返回按指定类型转换的 Variant (String)。 语法 StrConv(string, conversion, LCID) StrConv 函数的语法有下面的命名参数: 部分 说明 string 必要参数。要转换的字符串表达式。 conversion 必要参数。Integer。其值的和决定转换的类型。 LCID 可选的。如果与系统LocaleID不同,则为LocaleID(系统LocaleID为缺省值。) 设置值 conversion 参数的设置值为: 常数 值 说明 vbUpperCase 1 将字符串文字转成大写。 vbLowerCase 2 将字符串文字转成小写。 vbProperCase 3 将字符串中每个字的开头字母转成大写。 vbWide* 4* 将字符串中单字节字符转成双字节字符。 vbNarrow* 8* 将字符串中双字节字符转成单字节字符。 vbKatakana** 16** 将字符串中平假名字符转成片假名字符。 vbHiragana** 32** 将字符串中片假名字符转成平假名字符。 vbUnicode 64 根据系统的缺省码页将字符串转成 Unicode。 vbFromUnicode 128 将字符串由 Unicode 转成系统的缺省码页。 *应用到远东国别。 **仅应用到日本。 注意 这些常数是由 VBA 指定的。可以在程序中使用它们来替换真正的值。其中大部分是可以组合的,例如 vbUpperCase + vbWide,互斥的常数不能组合,例如 vbUnicode + vbFromUnicode。当在不适用的国别使用常数 vbWide、vbNarrow、vbKatakana,和 vbHiragana 时,就会导致运行时错误。 下面是一些一般情况下的有效分界符:Null (Chr$(0)),水平制表符 (Chr$(9)),换行 (Chr$(10)),垂直制表符 (Chr$(11)),换页 (Chr$(12)) ,回车 (Chr$(13)),空白 (SBCS) (Chr$(32))。在 DBCS中,空白的实际值会随国家/地区而不同。 说明 在把 ANSI 格式的 Byte 数组转换为字符串时,您应该使用 StrConv 函数。当您转换 Unicode 格式的这种数组时,使用赋值语句。
赵4老师 2018-01-11
  • 打赏
  • 举报
回复
哦,我这些都是C++语法。 至于VB6使用C++的UTF16字符串参数需要注意哪些细节,请自己搜。 不过我也只是怀疑问题出在这里。 实际情况不一定如此。
赵4老师 2018-01-11
  • 打赏
  • 举报
回复
百度搜“LPCSTR”
  • 打赏
  • 举报
回复
返回的err.lastdllerror是18,找不到文件应该是12003啊
赵4老师 2018-01-10
  • 打赏
  • 举报
回复
Contents Index Topic Contents Previous Topic: FtpDeleteFile Next Topic: FtpGetCurrentDirectory FtpFindFirstFile HINTERNET FtpFindFirstFile( IN HINTERNET hFtpSession, IN LPCSTR lpszSearchFile, OUT LPWIN32_FIND_DATA lpFindFileData, IN DWORD dwFlags, IN DWORD dwContext ); Searches the specified directory of the given FTP session. File and directory entries are returned to the application in theWIN32_FIND_DATA structure. Returns a valid handle for the request if the directory enumeration was started successfully; otherwise, returns NULL. To get a specific error message, callGetLastError. If the function finds no matching files, GetLastError returns ERROR_NO_MORE_FILES. hFtpSession Valid handle to an FTP session returned from InternetConnect. lpszSearchFile Address of a null-terminated string that specifies a valid directory path or file name for the FTP server's file system. The string can contain wildcards, but no blank spaces are allowed. If the value of lpszSearchFile is NULL or if it is an empty string, it will find the first file in the current directory on the server. lpFindFileData Address of aWIN32_FIND_DATA structure that receives information about the found file or directory. dwFlags Application-defined value that associates this search with any application. Can be one of the following values: INTERNET_FLAG_DONT_CACHE Does not add the returned entity to the cache. Identical to the preferred value, INTERNET_FLAG_NO_CACHE_WRITE. INTERNET_FLAG_HYPERLINK Forces a reload if there was no Expires time and no Last-Modified time returned from the server when determining whether to reload the item from the network. INTERNET_FLAG_MAKE_PERSISTENT No longer supported. INTERNET_FLAG_MUST_CACHE_REQUEST Causes a temporary file to be created if the file cannot be cached. Identical to the preferred value, INTERNET_FLAG_NEED_FILE. INTERNET_FLAG_NEED_FILE Causes a temporary file to be created if the file cannot be cached. INTERNET_FLAG_NO_CACHE_WRITE Does not add the returned entity to the cache. INTERNET_FLAG_RELOAD Forces a download of the requested file, object, or directory listing from the origin server, not from the cache. INTERNET_FLAG_RESYNCHRONIZE Causes the FTP resource to be reloaded from the server. dwContext Application-defined value that associates this search with any application data. This parameter is used only if the application has already called InternetSetStatusCallback to set up a status callback function. This function enumerates both files and directories. FtpFindFirstFile is similar to the Win32FindFirstFile function. Note, however, that only one FtpFindFirstFile can occur at a time within a given FTP session. The enumerations, therefore, are correlated with the FTP session handle. This is because the FTP protocol allows only a single directory enumeration per session. After calling FtpFindFirstFile and until calling InternetCloseHandle, the application cannot call FtpFindFirstFile again on the given FTP session handle. If a call is made to FtpFindFirstFile on that handle, the function will fail with ERROR_FTP_TRANSFER_IN_PROGRESS. After beginning a directory enumeration with FtpFindFirstFile, the InternetFindNextFile function can be used to continue the enumeration. The InternetCloseHandle function is used to close the handle returned from FtpFindFirstFile. If InternetCloseHandle closes the handle before InternetFindNextFile fails with ERROR_NO_MORE_FILES, the directory enumeration will be terminated. Because the FTP protocol provides no standard means of enumerating, some of the common information about files, such as file creation date and time, is not always available or correct. When this happens, FtpFindFirstFile and InternetFindNextFile fill in unavailable information with a best guess based on available information. For example, creation and last access dates will often be the same as the file's modification date. The application cannot call FtpFindFirstFile between calls to FtpOpenFile and InternetCloseHandle. See also InternetSetStatusCallback -------------------------------------------------------------------------------- Top of Page © 1997 Microsoft Corporation. All rights reserved. Terms of Use. hFindFile = FtpFindFirstFile(lnginetconn, L"/系统软件/sql server/2008/说明文档.doc", lpFileData, 0, 0)
  • 打赏
  • 举报
回复
最后一句啥意思,没看懂,谢谢
  • 打赏
  • 举报
回复
另外,我发现这台ftp上的其他文件也是这样的,只要给精确文件名就是找不到,又如hFindFile = FtpFindFirstFile(lnginetconn, "/系统软件/SAS/sample.zip", lpFileData, 0, 0) 也是返回0,除非随便加*就可以了。 若我换blnRC = FtpGetFile(lngInetconn, "/系统软件/SAS/sample.zip", "D:\SAS\sample.zip", 0, 0, 1, 0)可以下载到文件,说明我输入的文件名是正确的,通过flashfxp查看文件也是"/系统软件/sql server/2008/说明文档.doc", "/系统软件/SAS/sample.zip", 没有什么隐藏字符之类的。 因此,FtpFindFirstFile有点搞不明白为何找不到文件。
  • 打赏
  • 举报
回复
不是就是.doc扩展名,奇怪的是我的*星号随便放个位置,就能找到文件,难道有些ftp服务器端的文件名不适合于FtpFindFirstFile吗?
舉杯邀明月 2018-01-09
  • 打赏
  • 举报
回复
会不会实际扩展名是 .docx (Office 2007及更高版本)? Office 97/2K/2003 格式的文档,用的扩展名才是 .doc 。
  • 打赏
  • 举报
回复
谢谢,另外近期我发现FtpFindFirstFile精确查找文件时找不到,反馈hFindFile=0 ,而实际文件“”说明文档.doc“”在ftp上存在的, hFindFile = FtpFindFirstFile(lnginetconn, "/系统软件/sql server/2008/说明文档.doc", lpFileData, 0, 0) 若我在文件名任意位置加*,就返回正常,可以找到该文件,获取到文件,非常奇怪,这是为什么? hFindFile = FtpFindFirstFile(lnginetconn, "/系统软件/sql server/2008/说明文档.doc*", lpFileData, 0, 0) hFindFile = FtpFindFirstFile(lnginetconn, "/系统软件/sql server/2008/*说明文档.doc", lpFileData, 0, 0) hFindFile = FtpFindFirstFile(lnginetconn, "/系统软件/sql server/2008/*说明*文档.doc", lpFileData, 0, 0)
  • 打赏
  • 举报
回复
百度没有搜到类似的wininet断点续传
赵4老师 2018-01-08
  • 打赏
  • 举报
回复
百度搜“断点续传”?

7,762

社区成员

发帖
与我相关
我的任务
社区描述
VB 基础类
社区管理员
  • VB基础类社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧