WebClient Download Progress

sampoo

Active member
Joined
Jun 12, 2004
Messages
25
Location
Belgium
Programming Experience
3-5
Hi,

I know that the Webclient class in VB.NET is a very handy thing to download a file... But i need some more functionality. Is it somehow possible to retrieve the total filesize and the progress? Maybe the progress can be calculated by dividing the current filesize (on harddisk) by the total and multiplying by 100, then normally you should have percents...

Can it be done? Or should i look for another way to do it, and what do you suggest?

Thanks in advance....
 
try this function

Function DownloadChunks(ByVal sURL As String, ByVal pProgress As ProgressBar, ByVal Filename As String)
Dim wRemote As System.Net.WebRequest
Dim URLReq As HttpWebRequest
Dim URLRes As HttpWebResponse
Dim FileStreamer As New FileStream(Filename, FileMode.Create)
Dim bBuffer(999) As Byte
Dim iBytesRead As Integer

Try
URLReq = WebRequest.Create(sURL)
URLRes = URLReq.GetResponse
Dim sChunks As Stream = URLReq.GetResponse.GetResponseStream
pProgress.Maximum = URLRes.ContentLength

Do
iBytesRead = sChunks.Read(bBuffer, 0, 1000)
FileStreamer.Write(bBuffer, 0, iBytesRead)
If pProgress.Value + iBytesRead <= pProgress.Maximum Then
pProgress.Value += iBytesRead
Else
pProgress.Value = pProgress.Maximum
End If
Loop Until iBytesRead = 0
pProgress.Value = pProgress.Maximum
sChunks.Close()
FileStreamer.Close()
Return sResponseData
Catch
MsgBox(Err.Description)
End Try
End Function
 
Thanks, but I have a little problem: sResponseData seems not to be declared... And honestly i don't know what value this should have...

Thanks in advance

PS could you give a little more information about what the function does, because i have to store the downloaded file on disk... (on a specific location)
 
sorry my bad

Okay I've create a little sample program it's attached to this post. The only thing wrong in this program is that the form freezes during download, but the progressbar progresses a it should be, to resolve the freezing of the form you should use a thread which manages the downloadchunks function
 

Attachments

  • downloader.zip
    24.7 KB · Views: 722
Good idea, because i'm quite new to .NET (I always used VB6) and I don't really know how to make a thread... I checked the msdn lib but it's a little too confusing....
Is it possible to explain me that in simple, clear language?
Thanks a lot!
 
Back
Top