download file while showing progress bar

L

Loane Sharp

Hi there

I'm attempting to download a file from our server programmatically using an
internet connection. Ordinarily I would use WebClient.DownloadFile() but I
would like to show a progress bar during the download.

The code is given below.

Any ideas why it fails?

Best regards
Loane

[Other info: "pbr" is a progress bar initiated earlier in the code]

Dim fs_App As FileStream = New FileStream("C:\mainapp.zip", FileMode.Create)
datBuffer = New Byte(1023) {}
Dim reqSync_App As WebRequest =
WebRequest.Create("http://www.123.com/mainapp.zip")
Dim respSync_App As WebResponse = reqSync_App.GetResponse()
Dim strm_App As Stream = respSync_App.GetResponseStream()
Dim len_App As Double
Dim total, cnt_App As Integer
While total < pbrSync.Maximum
cnt_App = strm_App.Read(datBuffer, 0, 1024)
If cnt_App <= 0 Then Exit While
fs_App.Write(datBuffer, 0, cnt_App)
total += cnt_App
If total < pbrSync.Maximum Then pbrSync.Value = total
lblInfo7b.Text = "Downloading application file ... (" &
(total/1024).ToString("#,##0") & "KB of " &
(pbrSync.Maximum/1024).ToString("#,##0") & "KB) (" &
((total/1024)/(pbrSync.Maximum/1024)*100).ToString("##0") & "%)"
Application.DoEvents()
End While
respSync_App.Close()
strm_App.Close()
fs_App.Close()
 
M

Morten Wennevik

Hi Loane,

Please, at least attempt to format your code before posting. I find VB code hard to read as it is, and slightly cryptic reference names and bad formatting doesn't help :( An empty line in between does wonders to readability.

Nor do you tell us what kind of failure, error messages you get, which mean we more or less have to try out your code to see for yourself. Please include detailed error messages or what is or is not happening.

However from what I can read out of your code (actually by translating to C# and testing it), you never set Maximum in your progressbar so you will never see it move (I'm guassing pbrSync_App is a ProgressBar).

Before doing your while loop

pbrSync_App.Maximum = respSync_App.ContentLength
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top