Добавляешь на форму элементы: кнопку command1, micrisoft internet transfer inet1, Прогресс бар progress bar1, добавляешь код
Private Const INTERNET_OPEN_TYPE_PRECONFIG = 0
Private Const INTERNET_OPEN_TYPE_DIRECT = 1
Private Const INTERNET_OPEN_TYPE_PROXY = 3
Private Const scUserAgent = "VB Project"
Private Const INTERNET_FLAG_RELOAD = &H80000000
Private 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
Private Declare Function InternetOpenUrl Lib "wininet.dll" Alias "InternetOpenUrlA" (ByVal hOpen As Long, ByVal sUrl As String, ByVal sHeaders As String, ByVal lLength As Long, ByVal lFlags As Long, ByVal lContext As Long) As Long
Private Declare Function InternetReadFile Lib "wininet.dll" (ByVal hFile As Long, ByVal sBuffer As String, ByVal lNumBytesToRead As Long, lNumberOfBytesRead As Long) As Integer
Private Declare Function InternetCloseHandle Lib "wininet.dll" (ByVal hInet As Long) As Integer
Dim file_name As String
Private Function OpenURL(ByVal sUrl As String) As String
Dim hOpen As Long
Dim hOpenUrl As Long
Dim bDoLoop As Boolean
Dim bRet As Boolean
Dim sReadBuffer As String * 100
Dim lNumberOfBytesRead As Long
Dim sBuffer As String
Dim lLenthFile
Debug.Print Inet1.GetHeader("Content-length"
lLenthFile = CLng(Inet1.GetHeader("Content-length")
ProgressBar1.Max = lLenthFile / 100
hOpen = InternetOpen(scUserAgent, INTERNET_OPEN_TYPE_PRECONFIG, vbNullString, vbNullString, 0)
hOpenUrl = InternetOpenUrl(hOpen, sUrl, vbNullString, 0, INTERNET_FLAG_RELOAD, 0)
bDoLoop = True
While bDoLoop
sReadBuffer = vbNullString
bRet = InternetReadFile(hOpenUrl, sReadBuffer, Len(sReadBuffer), lNumberOfBytesRead)
sBuffer = sBuffer & Left$(sReadBuffer, lNumberOfBytesRead)
If Not CBool(lNumberOfBytesRead) Then bDoLoop = False
If ProgressBar1.Value < ProgressBar1.Max - 1 Then ProgressBar1.Value = ProgressBar1.Value + 1
Wend
If hOpenUrl <> 0 Then InternetCloseHandle (hOpenUrl)
If hOpen <> 0 Then InternetCloseHandle (hOpen)
OpenURL = sBuffer
Open App.Path & "\thisfile.xxx" For Binary As #1
Put 1, , sBuffer
Close #1
End Function
Private Sub Command1_Click()
file_name = "http://vbnet.ru/images/autors/user.gif"
Inet1.Execute file_name, "HEAD"
End Sub
Private Sub Inet1_StateChanged(ByVal State As Integer)
If State = 12 Then Call OpenURL(file_name)
End Sub
в коде кнопки меняешь путь файла, а также в коде вместо thisfile.xxx нужное имя файла
Если нужно скачивание в цикле лучше использовать таймер
Ответить
|