Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Progressbar 6

Status
Not open for further replies.

rikeman

Programmer
Sep 18, 2004
40
BE
Hello,

I've made a program that can download a file on a http site with inet.

--------------VisualBasicCodeBEGIN--------------

Private Sub Command1_Click()
Dim URL As String
Dim LocalFileName As String
Dim Contents() As Byte
URL = "Inet1.Protocol = icHTTP
Inet1.URL = URL
Contents() = Inet1.OpenURL(Inet1.URL, icByteArray)
Do Until Inet1Ready(False)
DoEvents: DoEvents: DoEvents: DoEvents
Loop
Open LocalFileName For Binary Access Write As #1
Put #1, , Contents()
Close #1
End Sub

Private Function Inet1Ready(ShowMessage As Boolean)
On Error Resume Next
If Inet1.StillExecuting Then
Inet1Ready = False
If ShowMessage Then

End If
Else
Inet1Ready = True
End If
End Function

--------------VisualBasicCodeEND--------------

This code works fine, but I want to add a progress bar or a textbox how many procent of the download is already done, and eventually the size of the file; does anyone know a code to do this or another possibility to download a file from a http site with a progress bar?

Rikeman.
 
The progress bar have a property called value. So to use it you have each time (this means a for/next loop) to assign a value both >= minValue and <= maxValue to the property value.

The way to use a pbar is:

-before download get the filesize in e.g bytes
-assign this value to progressbar.maxvalue
-add a timer control with interval e.g 1000.

The code in the timer should do:
-get the size of the file that is being written in the local disk
-assign it to the prograssbar.value

# Also leave the minvalue to the default which it zero.



-----Hope this helps
 
How can I get the file size of the file on the website and how can I get the file size of the file that is already downloaded on my computer? Cause the file will be written when it's completely downloaded, so I can't get the size of the file when it's still downloading.
 
It would be easier to do this in .NET
I suggest that you added a gif animation.
 
When using the openurl method, it will not return until the transfer is complete. The stillexecuting check isn't meaningful here. You can do an asychronous get, but I don't see a way to get the file size from the server unless you use the ftp options.

"I think we're all Bozos on this bus!" - Firesign Theatre [jester]
 
Can I use FTP controls (such as GET, SIZE etc. ) when downloading from a HTTP site? Can someone give me a concrete code to download a file from a HTTP site with a progress bar or something?
 
You can do this if you download the file using the Execute method instead of OpenURL method. Using this method, you can retrieve the filesize from the header information before downloading, and download progress as well during this process.

See the following article from vbip.com.
How to track the download progress
 
This code works onlyu for a web page, I want to made a code for a file on a webpage (ex.: song1.mp3)
 
Works fine for a file on a webpage. You just need minor modifications to a couple of places to deal with the fact that yu are downloading a byte array rather than a string. I suggest a quick look at the helpfiles.
 
I've got following code:

--------------VisualBasicCodeBEGIN--------------

Private m_lngDocSize As Long

Private Sub Command1_Click()
txtURL.text = " m_lngDocSize = 0
rtbDocument.Text = ""
ProgressBar1.Value = 0.001
lblProgressInfo.Caption = ""
Inet1.Protocol = icHTTP
If Len(txtURL.Text) > 0 Then
Inet1.Execute Trim$(txtURL.Text), "GET"
End If
End Sub

Private Sub Inet1_StateChanged(ByVal State As Integer)
Dim strText As String
Dim strBuffer As Byte
Dim sngProgerssValue As Single
Dim LocalFileName As String
LocalFileName = "c:\test.exe"
On Error Resume Next
Select Case State
Case icResponseCompleted
Do
DoEvents
strBuffer = Inet1.GetChunk(512, icByteArray)
strText = strText & strBuffer
If m_lngDocSize > 0 Then
If Len(strBuffer) > 0 Then
sngProgerssValue = Int((Len(strText) / m_lngDocSize) * 100)
End If
lblProgressInfo.Caption = "Downloaded " & CStr(Len(strText)) & _
" bytes (" & CStr(sngProgerssValue) & "%)"
ProgressBar1.Value = sngProgerssValue
End If
Loop Until Len(strBuffer) = 0
Open LocalFileName For Binary Access Write As #1
Put #1, , strText
Close #1
Case icResponseReceived
If m_lngDocSize = 0 Then
If Len(Inet1.GetHeader("Content-Length")) > 0 Then
m_lngDocSize = CLng(Inet1.GetHeader("Content-Length"))
End If
End If
End Select
End Sub

--------------VisualBasicCodeEND--------------

This code works not correct, when the download must finish, he downloads further, it never stops. Can someone give me the correct code? I think it's because he can't get the size of de exe file.
 
Now I've got following code:

--------------VisualBasicCodeBEGIN--------------

Private m_lngDocSize As Long

Private Sub Command1_Click()
m_lngDocSize = 0
rtbDocument.Text = ""
ProgressBar1.Value = 0.001
lblProgressInfo.Caption = ""
Inet1.Protocol = icHTTP
If Len(txtURL.Text) > 0 Then
Inet1.Execute Trim$(txtURL.Text), "GET"
End If
End Sub

Private Sub Inet1_StateChanged(ByVal State As Integer)
Dim strText As String
Dim strBuffer As Byte
Dim sngProgerssValue As Single
Dim LocalFileName As String
LocalFileName = "c:\test.ocx"
On Error Resume Next
Select Case State
Case icResponseCompleted
Do
DoEvents
strBuffer = Inet1.GetChunk(512, icByteArray)
strText = strText & strBuffer
If m_lngDocSize > 0 Then
If LenB(strBuffer) > 0 Then
sngProgerssValue = Int((LenB(strText) / m_lngDocSize) * 100)
End If
lblProgressInfo.Caption = "Downloaded " & CStr(LenB(strText)) & _
" bytes (" & CStr(sngProgerssValue) & "%)"
ProgressBar1.Value = sngProgerssValue
End If
Loop Until LenB(strBuffer) = 0
Open LocalFileName For Binary Access Write As #1
Put #1, , strText
Close #1
Case icResponseReceived
If m_lngDocSize = 0 Then
If LenB(Inet1.GetHeader("Content-Length")) > 0 Then
m_lngDocSize = CLng(Inet1.GetHeader("Content-Length"))
End If
End If
End Select
End Sub

--------------VisualBasicCodeEND--------------

He still downloads infinite. Can please someone give me the right code that works? Please.
 
You are mixing byte arrays and strings in improper way, thats why the code is not working...

See the following simplified code.

Start a new VB project. Add 2 Text boxes, 1 Command button, 1 Label control, 1 Inet control and 1 ProgressBar control.

Try running the the following code.
___
[tt]
Option Explicit
Private Sub Form_Load()
'initialize and stage controls
ScaleMode = vbPixels
Text1.Move 5, 115, 300, 20
Text2.Move 5, 135, 300, 20
ProgressBar1.Move 5, 155, 300, 20
Command1.Move 4, 175, 83, 23
Label1.Move 100, 180, 200, 20

'Text1.Locked = True
'Text2.Locked = True

Text1.Text = " Text2.Text = "C:\logo.gif"
ProgressBar1.Max = 100
Command1.Caption = "Download"
Label1.Caption = "<- Click to start downlaod"
End Sub


Private Sub Command1_Click()
Inet1.Protocol = icHTTP
Command1.Enabled = False
'start download
Inet1.Execute Trim$(Text1.Text), "GET"
End Sub

Private Sub Inet1_StateChanged(ByVal State As Integer)
Dim Buffer() As Byte 'buffer for receiving data from inet control.
On Error Resume Next
Select Case State
Case icResponseCompleted
Open Text2.Text For Binary As #1 'open output file
Do
DoEvents
Buffer = Inet1.GetChunk(512, icByteArray) 'get the data chunk
If UBound(Buffer) = -1 Then Exit Do 'no more data
Put #1, , Buffer 'write data to file
ProgressBar1.Value = Seek(1) - 1 'ammount of data downloaded
Label1.Caption = "Downloaded " & ProgressBar1.Value & " bytes, " & _
FormatPercent(ProgressBar1.Value / ProgressBar1.Max)
Loop
Close
Picture = LoadPicture(Text2.Text) 'display the downloaded image
Case icResponseReceived
If LenB(Inet1.GetHeader("Content-Length")) > 0 Then
ProgressBar1.Max = CLng(Inet1.GetHeader("Content-Length")) 'total size
End If
End Select
End Sub[/tt]
___

Works fine for me.
 
Hypetia, That is a nice piece of work. Thanks for sharing it! Here's your star :)

Thank you,

MrMajik

There are 10 types of people in the world:
Those that understand binary and those that don't.
 
I have a problem with this code. It works perfectly most of the time. However, sometimes it will not download. I have ZoneAlarm installed so I can see web traffic on this computer. When I click the download button sometimes it will download and sometimes it won't. I tried closing VB6 and opening the code again. Same thing. I also make an executable and it behaves the same way.

When it fails to download I can see there is a small amount of web traffic by watching the ZoneAlarm icon in the systray.

Do you know why this downloads sometimes and sometimes it won't?

Thank you.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top