7,762
社区成员
发帖
与我相关
我的任务
分享
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