[VB6] [Source] Download with ProgressBar and Stats with Inet 01-25-2011, 12:12 PM
#1
Download with ProgressBar and Stats with Inet
![[Image: rYjag.png]](http://i.imgur.com/rYjag.png)
Hello there, I made this a long time ago but modified a little today.
I also coded "Time remaining", but it was buggy and I can't debug properly because I don't have internet in my VM where I use VB6.
Thanks to:
[link=http://www.vbforums.com/showthread.php?t=311040]VB Forums[/link] for the basic code to download with ProgressBar
[link=http://www.multiupload.com/GEPY2BZ5NS]Download Exe[/link]
Spoiler: Source
Code:
Option Explicit
Private Declare Sub InitCommonControls Lib "comctl32.dll" ()
Dim DownloadProgress As Integer, CurrentSize As Double, Seconds As Long
Dim tempSize As Double, lngFileLength As Double
Private Sub cmdDownload_Click()
On Error GoTo ErrReturn
If tURL.Text = "http://" Then Exit Sub
CancelError = True
DialogTitle = "Choose a location to Save"
FileName = Right(tURL.Text, Len(tURL.Text) - InStrRev(tURL.Text, "/"))
Dim ext As String
ext = Right(tURL.Text, Len(tURL.Text) - InStrRev(tURL.Text, "."))
Filter = "." & ext & "|." & ext & "|All Files|*.*"
InitDir = Environ("USERPROFILE") & "\Desktop"
ShowSave
ProgressBar1.Visible = True
ProgressBar1.Value = 0
Timer1.Enabled = True
DownloadFile tURL.Text, FileName
Timer1.Enabled = False
If Seconds <= 60 Then
lRem.Caption = "Download Complete" & vbCrLf & "Time Elpased: " & Seconds & " seconds"
ElseIf Seconds <= 360 And Seconds >= 61 Then
Dim Minutes As Byte
Minutes = Seconds / 60
lRem.Caption = "Download Complete" & vbCrLf & "Time Elapsed: " & Minutes & " minutes" & Seconds & " seconds"
End If
lbl.Caption = "Click here to open File location" & vbCrLf & "Total Size: " & Round(CurrentSize / 1024, 2) & "Mb"
ProgressBar1.Visible = False
Exit Sub
ErrReturn:
End Sub
Private Sub Form_Initialize()
InitCommonControls
End Sub
Private Sub Form_Load()
pic.BackColor = RGB(220, 220, 220)
ProgressBar1.Visible = False
End Sub
Public Sub DownloadFile(strURL As String, strDestination As String)
Const CHUNK_SIZE As Long = 1024
Dim intFile As Integer, lngBytesReceived As Double
Dim strHeader As String, b() As Byte, i As Integer
DoEvents
With Inet1
.URL = strURL
.Execute , "GET", , "Range: bytes=" & CStr(lngBytesReceived) & "-" & vbCrLf
While .StillExecuting
DoEvents
Wend
strHeader = .GetHeader
End With
strHeader = Inet1.GetHeader("Content-Length")
lngFileLength = Val(strHeader)
DoEvents
If lngFileLength = 0 Then Exit Sub
lngBytesReceived = 0
intFile = FreeFile()
Open strDestination For Binary Access Write As #intFile
Do
b = Inet1.GetChunk(CHUNK_SIZE, icByteArray)
Put #intFile, , b
lngBytesReceived = lngBytesReceived + UBound(b, 1) + 1
DownloadProgress = Round((lngBytesReceived / lngFileLength) * 100, 1)
ProgressBar1.Value = Round(DownloadProgress)
CurrentSize = Round((lngBytesReceived / 1024), 2)
lbl.Caption = CurrentSize & " kB of " & Round((lngFileLength / 1024), 2) & " kB Downloaded"
DoEvents
Loop While UBound(b, 1) > 0
Close #intFile
End Sub
Private Sub lbl_Click()
Shell "explorer.exe " & Left(FileName, InStrRev(FileName, "\")), vbNormalFocus
End Sub
Private Sub Timer1_Timer()
On Error GoTo er
lRem.Caption = DownloadProgress & "% Done" & vbCrLf & "Download Speed: " & CurrentSize - tempSize & " Kb/s"
Seconds = Seconds + 1
tempSize = CurrentSize
er:
End Sub
Private Sub Form_Unload(Cancel As Integer)
Unload Me
End
End Sub![[Image: rytwG00.png]](http://i.imgur.com/rytwG00.png)
Redcat Revolution!





![[+]](https://sinister.li/images/modern/collapse_collapsed.png)
![[Image: 5DQAS.gif]](http://dragcave.net/image/5DQAS.gif)

Thx for the share^^