'----------------------------- Form1 -----------------------------
Option Explicit
Private Declare Sub InternetCloseHandle Lib "wininet.dll" ( _
ByVal hInet As Long)
Private Declare Function InternetOpenA Lib "wininet.dll" ( _
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 InternetOpenUrlA Lib "wininet.dll" ( _
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 Sub InternetReadFile Lib "wininet.dll" ( _
ByVal hFile As Long, ByVal sBuffer As String, _
ByVal lNumBytesToRead As Long, lNumberOfBytesRead As Long)
Public Enum InternetOpenType
IOTPreconfig = 0
IOTDirect = 1
IOTProxy = 3
End Enum
Private Const INET_RELOAD = &H80000000
Private Cancel_Download As Boolean
Public Function DownloadBytes(Bytes As Long)
Me.Caption = "Download: " & Int(Bytes / 1024) & " KBytes"
End Function
Public Function DownloadFile(ByVal URL As String, FileName As String, Optional ByVal OpenType As InternetOpenType = IOTPreconfig)
Dim hInet As Long
Dim hURL As Long
Dim Buffer As String * 2048
Dim Bytes As Long
Dim TotalBytes As Long
Cancel_Download = False
hInet = InternetOpenA("VB-Program", OpenType, vbNullString, vbNullString, 0)
hURL = InternetOpenUrlA(hInet, URL, vbNullString, 0, INET_RELOAD, 0)
Open FileName For Binary As #1
Do
InternetReadFile hURL, Buffer, Len(Buffer), Bytes
Put #1, , Left$(Buffer, Bytes)
TotalBytes = TotalBytes + Len(Left$(Buffer, Bytes))
DoEvents
Call DownloadBytes(TotalBytes)
Loop Until Bytes = 0 Or Cancel_Download = True
Close #1
InternetCloseHandle hURL
InternetCloseHandle hInet
End Function
Private Sub Command1_Click()
DownloadFile "
http://www.navn.dk/file.exe", "C:\file.exe"
End Sub
Private Sub Command2_Click()
Cancel_Download = True
End Sub
Private Sub Form_Load()
Command1.Caption = "Start"
Command2.Caption = "Annuller"
End Sub
'----------------------------- Form1 -----------------------------