'添加 internet transfer control 6.0 和 windows commom controls 6.0
'form代碼:
Private Sub cmdGET_Click()
StartDownLoad txtURL
End Sub
Private Sub Form_Load()
savefile.Text = App.Path
End Sub
Private Sub StartDownLoad(ByVal Geturl As String)
Dim spo%, filename$
Dim fso, f
Set fso = CreateObject("Scripting.FileSystemObject")
If Not fso.FolderExists(App.Path & "\download") Then Set f = fso.CreateFolder(App.Path & "\download")
spo = InStrRev(Geturl, "/")
filename = Right(Geturl, Len(Geturl) - spo) '獲取文件名
savefile.Text = App.Path & "\download\" & filename
Inet1.Execute Geturl, "get" '開始下載
End Sub
Private Sub Inet1_StateChanged(ByVal State As Integer)
'State = 12 時,用 GetChunk 方法檢索服務器的響應。
Dim vtData() As Byte
Select Case State
'...沒有列舉其它情況。
Case icError '11
'出現錯誤時,返回 ResponseCode 和 ResponseInfo。
vtData = Inet1.ResponseCode & ":" & Inet1.ResponseInfo
Case icResponseCompleted ' 12
Dim bDone As Boolean: bDone = False
'取得第壹個塊。
vtData() = Inet1.GetChunk(1024, 1)
DoEvents
Open savefile.Text For Binary Access Write As #1 '設置保存路徑文件後開始保存
'獲取下載文件長度
If Len(Inet1.GetHeader("Content-Length")) > 0 Then ProgressBar1.Max = CLng(Inet1.GetHeader("Content-Length"))
'循環分塊下載
Do While Not bDone
Put #1, Loc(1) + 1, vtData()
vtData() = Inet1.GetChunk(1024, 1)
DoEvents
ProgressBar1.Value = Loc(1) '設置進度條長度
If Loc(1) >= ProgressBar1.Max Then bDone = True
Loop
Close #1
MsgBox "下載完成", vbInformation, "通知"
End Select
End Sub