Downloader Example..

Lanc1988

Well-known member
Joined
Nov 27, 2003
Messages
508
I have been trying to make this for a long time and have looked for an example of it but every example I find isnt what I need to do.

I have a file at the following url:
http://www.rs-sidekick.com/downloads/RS_SideKick_099.msi

What I want is a form with a progress bar and a button that says Download. Im not very good when working with the internet using vb.net so if someone could make me a basic example of this or if you know of where i can find an example that does this, please post it.

Thanks
 
This function was posted by someone on this forum, but I cant find the thread so sorry for not being able to give credit :)

Code:
         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

Im currently writing an updater app for a friend, and this works perfectly for it - it should work fine for you :)
sURL is the URL of the file to download, pProgress is the progress bar that youre using, and Filename is the filename to download to.
Remember to put imports System.Net, and imports System.IO ;)
 
Since my updater I found earlier isnt working quite right, i tried out your code you posted and it works perfectly.. except while its downloading it kinda like freezes the window and i cant move the window around or click any buttons on the form until it is done. Kinda like its taking up all of my memory or something..

I didnt know where to put the code so I just took off the Function lines at the top and bottom and placed the rest of it under a button.. could that be way its doing that? How do I put the code in with the Function lines?

Maybe you could post the project you put the updater code into or create a simple one that I could see working right. Thanks :)

(the file i want to download is at http://www.rs-sidekick.com/downloads/rs_sidekick_099.msi)
 
Last edited by a moderator:
I had the same problem in my app - the download was quite big so it froze for a long time. The solution I have used is multithreading.

The problem for me here was that afaik you cant start a function as the thread, only a Sub. So the way Ive done it is like this:

Code:
       Private Sub cmdDownload_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
       Dim downloadThread As New Thread(AddressOf StartDLThread)
       downloadThread.Start()
       End Sub
       
       Sub StartDLThread()
       DownloadChunks("http://www.rs-sidekick.com/downloads/rs_sidekick_099.msi", pProgress, "Path to Download to")
       End Sub
       
       Function DownloadChunks(ByVal sURL As String, ByVal pProgress As ProgressBar, ByVal Filename As String)
    You already have this function :)
           End Function

Remember if you use this solution to put Imports System.Threading

(sorry I cant use the proper VB tags to make it readable, every time I try to use them it puts all of the code onto one line :( )

Hope this helps ;)
 
Yay! It works exactly how I need it to.. now I just wanna have it start the download on Form1 load event instead of Button1.Click event and also is there a way to have a cancel button that will stop the download?
 
Hmm, well I havent implemented a cancel button yet, but off the top of my head you could put this in a cancel button (its not tested, mind.. off the top of my head :))

threadName.Abort() to kill the thread
if file.exists(filePath) then
kill(filePath) if any of the file has been downloaded, might as well delete it
else
exit sub
end if

filePath being the path of the file on your HD.

Hope this is of any help - but I think there may be a better way to end the download than just aborting the thread - Im new to .NET myself. Maybe someone else will be able to give better input on that ;)
 
i tried adding the code to show percent complete and it definately isnt showing how much complete it is, cuz the percent is going backwards..

Heres what i used:

Label2.Text = Fix((pProgress.Value / URLRes.ContentLength) * 100) & "%"
 
Oops - when I typed the code from the top of my head I forgot what I called the thread in my example - try replacing threadName with downloadThread.
 
it still says that downloadThread is not declared.. i guess its because since all the code that declares that stuff is under the Function tags
 
Your percentage code works perfectly for me.

I put it in the downloadChunks function, after the line
" pProgress.Value += iBytesRead"
and before the line
"else"

It worked with no problems :)

Edit: Regarding the thread problem.. Sorry I totally forgot abotu that. Try taking the line "Dim downloadThread....." with

"Public downloadThread As New Thread(AddressOf StartDLThread)"

But put that code outside of any function or sub. I think that should fix it :)
 
Last edited by a moderator:
yeah, i just coped this line of code:
Dim downloadThread As New Thread(AddressOf StartDLThread)

to the top of my projects code under the Windows Form Designer Generated Code section and now the cancel button works and so does the percent complete code

so thanks alot :D

Do you happen to know how to add a time remaining thing? it would probably be kinda difficult so Ill probably just add a Time Elapsed instead, unless someone knows how.
 
Using math you can work out the download speed per second, so maybe you could use that to estimate an ETA? Unfortunately math is far from being a strong point for me, so I cant help there :P Im very sure it is possible though ;)
 
yeah, im pretty sure its possible but im not sure how to do it yet, ill have to think about it.

anyway, thanks alot ;)
 
hello, thanks for the example... its works great...

i bin working that the program support pause/resume, but i was not able to do it, some one can make it?

thanks...
 
Wouldnt you need to send the desired file position back to the server... not sure about the protocol used but if the server allows resuming it shouldnt be a prob... :)
 
Back
Top