生活随笔
收集整理的這篇文章主要介紹了
vb6实现下载带进度
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
可以通過修改sleep的時間和每次下載的字節數來控制下載速度,窗體只有一個label1和一個command1
Option Explicit
Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds
As Long)Dim mDownLoaddComPlete
As BooleanPrivate Sub Command1_Click
()Dim Filename
As StringDim Url
As StringDim fso
Dim aso
Dim http
Dim current
Dim start
Dim i
Dim total
Dim temp
As StringDim range
As StringDim DnbyteDnbyte
= 20480 Url
= "http://111.230.45.209:5500/doorlocktype/btlock57L_1027K.rar"Filename
= "E
:\下載\btlock57L_1027K.rar"
If Not Left(Url
, 7) = "http://" ThenUrl
= "http://" & Url
End IfSet fso
= CreateObject
("Scripting.FileSystemObject")Set aso
= CreateObject
("ADODB.Stream")Set http
= CreateObject
("Microsoft.XMLHTTP")If Dir
(Filename
) <> "" ThenKill Filename
End Iffso.CreateTextFile
(Filename
).
Closeaso.
Type = 1 aso.
openaso.loadfromfile Filename start
= 0current
= 0Label1
= "開始下載:"
& Filename
Dohttp.
open "GET", Url
, Truehttp.setrequestheader
"Range", "bytes=" & start
& "-" & CStr(start
+ Dnbyte
)http.setrequestheader
"Content-Type:", "application/octet-stream"http.send
For i
= 1 To 120 If http.ReadyState
= 4 Then Exit For Sleep 10 DoEvents
NextIf http.
Status = 416 ThenmDownLoaddComPlete
= TrueExit Do End IfIf http.
Status = 403 ThenMsgBox "連接數過多
", vbInformation, "提示"
Exit DoEnd IfIf http.
Status = 404 ThenMsgBox "文件無法找到
", vbInformation, "提示"
Exit DoEnd IfIf Not http.ReadyState
= 4 ThenMsgBox "下載文件超時
", vbInformation, "提示"
Exit DoEnd IfIf http.
Status > 299 ThenMsgBox Url
& "未知錯誤:
" & http.StatusText & "(" & http.Status & ")", vbInformation, "提示"
Exit DoEnd IfIf Not http.
Status = 206 ThenMsgBox "不支持斷點續傳
", vbInformation, "提示"
Exit DoEnd Ifaso.position
= start aso.
Write http.responsebody range
= http.getresponseheader
("Content-Range") If range
= "" ThenMsgBox "無法獲取文件大小!
", vbInformation, "提示"
End Iftemp
= Mid(range
, InStr
(range
, "-") + 1) current
= CLng(Left(temp
, InStr
(temp
, "/") - 1)) total
= CLng(Mid(temp
, InStr
(temp
, "/") + 1)) Label1
= Int(current
/ total
* 100) & "%(" & current
& "/" & total
& ")"If total
- current
= 1 ThenmDownLoaddComPlete
= TrueExit Do End Ifstart
= start
+ Dnbyte DoEvents
Loop While Trueaso.SaveToFile Filename
, 2 aso.
CloseSet aso
= NothingSet fso
= NothingIf mDownLoaddComPlete
= True ThenLabel1
= "下載完成!
(共
" & total & "字節
)"
End IfEnd Sub
總結
以上是生活随笔為你收集整理的vb6实现下载带进度的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。