Here is a sample project I did up that uses a class I wrote (WebFileDownloader)
The WebFileDownloader class provides methods for downloading a file from a URL and firing events to update progress in the GUI on a progress bar or whatever you may like.
The standard WebClient class in the .NET framework has a method for downloading a file, but there is no indication of its progress as it downloads, which is why I wrote this up.
Known limitations:
progress is returned as a long, and a progress bar takes an integer, so in the sample I convert the long to integer, which could error in BIG downloaded files.. a better solution would be to use the filesize being downloaded and calculate a good maximum to set the progress bar to. However this is GUI related, and not related to the WebFileDownloader class itself. (Just wanted to point it out)
'SUB MAIN WHERE WE ENABLE VISUAL STYLES, AND RUN MAIN FORM
Public Shared Sub Main()
Application.EnableVisualStyles()
Application.DoEvents()
Application.Run(New frmMain)
Application.Exit()
End Sub
It also requires you set the buttons flatstyle property to system instead of standard. (any control that has a flatstyle property should be set to system to render it in XP theme).
.NET 2005 supports XP theme right from the get go, without needing to call this.
Thanks again, I remember you helping me with this problem on an earlier date. I do have a quesition though, of course. How does this handle proxy servers or does it? If not how should I go about implementing that functionality?
Thanks,
Christian
In life you can be sure of only two things... death and taxes. I'll take death.
I have a question: When the user clicks the close button (or exits application) during a download, download procedure keeps going on.
I'd like to know how to add the following feature:
When the application is stopped somehow
(by user, by system or other failure)
1. Stop the download process
2. Delete the file that was downloaded partially
Excellent class, I just used it for a small updater program, however i did notice one thing, the downloader does not stop when the form is closed. The application remains present in the taskmanager until the download completes.
I was wondering is anyone had any insight on how to make the downloader stop. I've never really used any web/http classes before they aren't really my forte. Maybe some kind of cancel event or something could be raised? I'm not really sure where this would be coded though. And i guess we'd have to clean up the partial stream. I'm kinda tired right now as I'm posting this so my brains not working 100%.
Maybe just adding a cancel event that changes a member variable blnCancel = True and modifying some of the loops to check that variable will work. Then raising the event when the form closes would work, might not be the cleanest but I'll try it after I wake up. If it works I'll repost modified code if that's ok.
Another thing I noticed is that download speed seem to dramatically increase after the downloaded stream starts reporting its size in MB rather than KB on my cable modem. I would say on average of 3-5x the speed it takes to go from 0 to 1MB. Not sure why this might be.
Anyway again, thanks for the class, saved me some time!
well generally you would not want to allow the form to be closed while downloading. However if you want to offer a cancel option, you could simply set a boolean variable in the downloader class to indicate you want to cancel the download. Then in the DownloadFileWithProgress routine, check this boolean value in the do loop, and drop out of it if infact the cancel var is set to true. You may also want to delete the semi downloaded file when this happens, and maybe even add a new event like "DownloadCancelled". I did try to add a pause feature in recently, but so far it is still a little buggy.
Also I am not sure about what you say regarding the download speeds when the file is over 1MB downloaded. I don't notice any significant speed changes between 0-1 MB and then larger... How big are the files you are downloading?
well generally you would not want to allow the form to be closed while downloading. However if you want to offer a cancel option, you could simply set a boolean variable in the downloader class to indicate you want to cancel the download. Then in the DownloadFileWithProgress routine, check this boolean value in the do loop, and drop out of it if infact the cancel var is set to true. You may also want to delete the semi downloaded file when this happens, and maybe even add a new event like "DownloadCancelled". I did try to add a pause feature in recently, but so far it is still a little buggy.
Also I am not sure about what you say regarding the download speeds when the file is over 1MB downloaded. I don't notice any significant speed changes between 0-1 MB and then larger... How big are the files you are downloading?
I am also noticing this, just reported it in another topic. It goes from 60 kbps to 700 kbps after it switches to MB. Maybe there is a way to keep it at MB from the beginning? (file=3mb).
this will make the label only report the bytes download, and not format them into MB, KB, etc.. for display purposes. Again I think you are just thinking it looks faster before it has DLed a MB of the file.
Yes I am sure because I use an external program to measure download speeds (DU meter 3.07). Thats how I noticed the increase from 60 KB/s to 700 KB/s.
I'll try the code, do I need to replace the first line you gave?
Also there are some ! according to VB with the default project. (I am now using an exact copy of webfiledownloader.vb in my program)
Warning 1 Variable 'FS' is used before it has been assigned a value. A null reference exception could result at runtime. I:\Documents and Settings\Sebas\Mijn documenten\Visual Studio 2005\Projects\FileTransfer\FileTransfer\WebFileDownloader.vb 70 21 FileTransfer
Warning 2 Function 'FormatFileSize' doesn't return a value on all code paths. A null reference exception could occur at run time when the result is used. I:\Documents and Settings\Sebas\Mijn documenten\Visual Studio 2005\Projects\FileTransfer\FileTransfer\WebFileDownloader.vb 99 5 FileTransfer
Ok I replaced the line, now it's slow all the way. I think updating for every byte might be a little to much for the app. Now I reached 61-117 KBps, my maximum speed (of the same file) is 1058 KB/s. Do you know the line to format in MB instead of autoformat?
edit: when I remove the line entirely I get speeds from 640 - 890. Then the problem is solved... so the byte reporting is causing it to slow down.
if you run a tight loop that scanned every file on your computer, it would run a bunch faster than if the same loop was also updating a label with the current file it was scanning.
If you still want to show UI progress, but speed it up, you could use some sort of if statement to determine if you should update the GUI with progress or not. Since the default is to update after even chunk of the download is completed, perhaps only have it update 1 out of every 4 times the event fires or something.
if you run a tight loop that scanned every file on your computer, it would run a bunch faster than if the same loop was also updating a label with the current file it was scanning.
If you still want to show UI progress, but speed it up, you could use some sort of if statement to determine if you should update the GUI with progress or not. Since the default is to update after even chunk of the download is completed, perhaps only have it update 1 out of every 4 times the event fires or something.
Ok but it is still weird that if it switches from bytes/kb to MB then after that it's fine... Maybe because the mb figures have to change less often?
I'm new to vb 2005, thought the format was included in vb 2005. I see that you created it yourself.
So I started editing, just doing this:
Code:
Else
Select Case Size / KB
'Case Is < 1000
' Return (Size / KB).ToString("N") & "KB"
Case Is < 1000000
Return (Size / MB).ToString("N") & "MB"
Case Is < 10000000
Return (Size / MB / KB).ToString("N") & "GB"
End Select
Solves the slowdown for me. (I commented the kb case).
Last edited by sdk1985; May 1st, 2007 at 04:41 PM.
Reason: found solution
well generally you would not want to allow the form to be closed while downloading. However if you want to offer a cancel option, you could simply set a boolean variable in the downloader class to indicate you want to cancel the download. Then in the DownloadFileWithProgress routine, check this boolean value in the do loop, and drop out of it if infact the cancel var is set to true. You may also want to delete the semi downloaded file when this happens, and maybe even add a new event like "DownloadCancelled". I did try to add a pause feature in recently, but so far it is still a little buggy.
(...)
As I'm a new coder: can you help me a bit more to include this code to my project? Maybe you can give some more clues, so that I can learn at the same time.
Steps (according to what you've mentioned):
1. Setting a boolean class for cancelling download
2. Include this class within the DownloadFileWithProgress routine (inside do loop)
3. Add DownloadCancelled event for erasing partially downloaded file or other stuff on "post partial-download event".
Thanks a lot!
Vista Error #01: Insufficient money spent in hardware.
Hi Kleinma,
thanks for the great code,
I tried to adapt it to my case however I ran into an issue.
My links are asp type of links and they open a doc file in MS Word.
So they have the "?" charachter in the URL.
When I enter them on the app it's telling me that there are illegal charchters.
Any idea how this can be fixed ?
thanks
Last edited by Zoroxeus; Jun 3rd, 2007 at 09:34 AM.
I need to download the file from the site which ask for userid and password
i use the following code to download the file( Without userid and password)
Code:
Dim wClient As New System.Net.WebClient
Dim add As String = "http://203.199.49.90/XMLXX/CCnn/15KT/Pnn_001.zip"
Try
download:
wClient.DownloadFile(add, "n:\genbas\Pnn_011.zip")
But how i can pass the userid and passwords To the above string
Plase help
I need to download the file from the site which ask for userid and password
i use the following code to download the file( Without userid and password)
Code:
Dim wClient As New System.Net.WebClient
Dim add As String = "http://203.199.49.90/XMLXX/CCnn/15KT/Pnn_001.zip"
Try
download:
wClient.DownloadFile(add, "n:\genbas\Pnn_011.zip")
But how i can pass the userid and passwords To the above string
Plase help
Specify the user id/password in the credentials property of the webclient before making the download call.
Code:
Dim wClient As New System.Net.WebClient
Dim add As String = "http://203.199.49.90/XMLXX/CCnn/15KT/Pnn_001.zip"
Try
wClient.Credentials = New Net.NetworkCredential("userid", "password")
wClient.DownloadFile(add, "n:\genbas\Pnn_011.zip")
1) download and open up the my example code
2) go into the class file called WebFileDownloader
3) find the routine called DownloadFileWithProgress
4) find the line of code mentioned above
wRemote = WebRequest.Create(URL)
5) insert this code after it
wRemote.Credentials = New Net.NetworkCredential("USERNAME", "PASSWORD")
6) change USERNAME and PASSWORD to actual valid values
7) test code against site that needs authentication
1) download and open up the my example code
2) go into the class file called WebFileDownloader
3) find the routine called DownloadFileWithProgress
4) find the line of code mentioned above
wRemote = WebRequest.Create(URL)
5) insert this code after it
wRemote.Credentials = New Net.NetworkCredential("USERNAME", "PASSWORD")
6) change USERNAME and PASSWORD to actual valid values
7) test code against site that needs authentication
Its not working i have try it just now
Code:
FS = New FileStream(Location, FileMode.Create, FileAccess.Write)
wRemote = WebRequest.Create(URL)
wRemote.Credentials = New Net.NetworkCredential("myname", "mypass")
Dim myWebResponse As WebResponse = wRemote.GetResponse
RaiseEvent FileDownloadSizeObtained(myWebResponse.ContentLength)
Dim sChunks As Stream = myWebResponse.GetResponseStream
Do
iBytesRead = sChunks.Read(bBuffer, 0, 256)
FS.Write(bBuffer, 0, iBytesRead)
iTotalBytesRead += iBytesRead
If myWebResponse.ContentLength < iTotalBytesRead Then
RaiseEvent AmountDownloadedChanged(myWebResponse.ContentLength)
Else
they might be doing some sort of redirect on the server, so you may only be getting the first response.
I dont know how rapidshare servers work and if they just serve out the file when you navigate to it, or do some sort of redirect. Chances are if you are getting 5KB, its HTML.
they might be doing some sort of redirect on the server, so you may only be getting the first response.
I dont know how rapidshare servers work and if they just serve out the file when you navigate to it, or do some sort of redirect. Chances are if you are getting 5KB, its HTML.
How ever is any chance to get close have a look on this problem pls? Im really desperate to get it work. Im looking for solution almoust 2 months. I can also pay for your time if is it reuquire.... Thx
well just by looking at the site, they defenitly do some funky redirects (at least when u use the free downloading service). They might even do direct response writing of the files to prevent people from direct linking.
If you want to PM me one of your acct id/passwords, I will test trying to make it work, as it may be easier when you have an actual paid acct to work with. I can't promise I will be able to get back to you right away, but if you PM me with that info, I will look at it when I have a few free minutes.
That code from Karl will actually error.. it should be written like this:
Code:
Public Function IsConnectionAvailable() As Boolean
' Returns True if connection is available
' Replace www.yoursite.com with a site that
' is guaranteed to be online - perhaps your
' corporate site, or microsoft.com
Dim objUrl As New System.Uri("http://www.yoursite.com/")
' Setup WebRequest
Dim objWebReq As System.Net.WebRequest = Nothing
objWebReq = System.Net.WebRequest.Create(objUrl)
Dim objResp As System.Net.WebResponse = Nothing
Try
' Attempt to get response and return True
objResp = objWebReq.GetResponse
objResp.Close()
objWebReq = Nothing
Return True
Catch ex As Exception
' Error, exit and return False
If objResp IsNot Nothing Then
objResp.Close()
objWebReq = Nothing
Return False
End If
End Try
End Function