'函数名: DownloadFile
'功能: 下载指定URL的文件到硬盘目录
'作者: Sakurako
'参数: SourceURL:网络上的文件地址
' FileName:要保存的文件路径 文件名
'返回值: 成功:True;失败:False
'测试状态: 已测试
'时间: 2004-10-06
'其他:
Private Declare Function URLDownloadToFile Lib "urlmon.dll" Alias "URLDownloadToFileA" (ByVal pCaller As Long, ByVal szURL As String, ByVal szFileName As String, ByVal dwResv As Long, ByVal lpfnCB As Long) As Long
Private Const BINDF_GETNEWESTVERSION As Long = &H10
Private Const ERROR_SUCCESS As Long = 0
Public Function DownloadFile(ByVal SourceURL As String, ByVal FileName As String) As Boolean
DoEvents
DownloadFile = URLDownloadToFile(0&, SourceURL, FileName, BINDF_GETNEWESTVERSION, 0&) = ERROR_SUCCESS
End Function
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" (ByRef hInet As Long) As Long
Private Declare Function InternetReadFile Lib "wininet" (ByVal hFile As Long, 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
Private Declare Function HttpQueryInfo Lib "wininet.dll" Alias "HttpQueryInfoA" (ByVal hHttpRequest As Long, ByVal lInfoLevel As Long, ByVal sBuffer As Any, ByRef lBufferLength As Long, ByRef lIndex As Long) As Integer
Public Function ReadUrl(ByVal sUrl As String, Optional vFileName As _
Variant) As Boolean
Dim sReadBuffer As String * 2048 ' Bytes to read from one call
Dim lNumberOfBytesRead As Long ' Bytes read from call to InternetReadFile
Dim lTotalBytesRead As Long ' Total bytes read
Dim bDoLoop As Boolean ' Return value from InternetReadFile
Dim bReadInternetFile As Boolean
Dim bWriteToFile As Boolean
On Error GoTo errReadUrl
Screen.MousePointer = vbHourglass
SetStatus "Opening Url..."
If Not IsMissing(vFileName) Then
Dim iFileNum As Integer
iFileNum = FreeFile
Open CStr(vFileName) For Binary As iFileNum
bWriteToFile = True
End If
hUrlFile = InternetOpenUrl(hInternetSession, sUrl, vbNullString, 0,_
INTERNET_FLAG_RELOAD, 0)
If CBool(hUrlFile) Then
sContents = scBlankStr
bDoLoop = True
While bDoLoop
sReadBuffer = scBlankStr
bDoLoop = InternetReadFile(hUrlFile, sReadBuffer, Len(sReadBuffer), _
lNumberOfBytesRead)
If Not CBool(bDoLoop) Then CheckError
lTotalBytesRead = lTotalBytesRead + lNumberOfBytesRead
SetStatus "Reading Url: " & CStr(lTotalBytesRead) & " Bytes read..."
If CBool(lNumberOfBytesRead) Then
If bWriteToFile Then
Put #iFileNum, , sReadBuffer
Else
sContents = sContents & Left$(sReadBuffer,lNumberOfBytesRead)
End If
Else
bDoLoop = False
bReadInternetFile = True
End If
Wend
InternetCloseHandle (hUrlFile)
ReadUrl = True
Else
CheckError
End If
If bWriteToFile Then Close
SetStatus "Ready"
Screen.MousePointer = vbDefault
Exit Function
errReadUrl:
sLastError = Error$(Err)
Screen.MousePointer = vbDefault
Exit Function
End Function