-
1 Attachment(s)
Download Files From Web With Progressbar
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)
Also make sure the URL is a full URL (ie http://www.mysite.com/file.zip and not www.mysite.com/file.zip)
Please post any corrections you may find. I use this code all the time, but that doesn't mean there isn't a bug or 2 lurking somewhere ;)
-
Re: Download Files From Web With Progressbar
Thank you for the sample project.
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
-
Re: Download Files From Web With Progressbar
I noticed the buttons and progress bar in this project have the "XP" look.
How did you do that?
I can't find an option anywhere in Visual Studio?
Edit: looking through the zip file I can't find any use of manifest files or anything either?
-
Re: Download Files From Web With Progressbar
I enable visual styles in the Sub Main routine.
This code here
VB Code:
'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.
-
Re: Download Files From Web With Progressbar
how can i add download time estimation to this code ? i'm a beginner please help me :)
-
Re: Download Files From Web With Progressbar
Kleinma,
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
-
Re: Download Files From Web With Progressbar
It currently doesn't and I don't have a proxy server to test any code against.
However I would imagine it would probably be as simple as setting the proxy property of the WebRequest object in the DownloadFileWithProgress routine.
After the line
wRemote = WebRequest.Create(URL)
try adding
wRemote.Proxy = new WebProxy()
but specify the needed information in one of the WebProxy overloaded constructors.
That should be all there is to it.
Let me know how you make out.
-
Re: Download Files From Web With Progressbar
-
Re: Download Files From Web With Progressbar
Hi, I tried adding this to program to auto update and noticed that WebFileDownloader doesn't seem to work. I use VS 2005 and it says:
Code:
'Type 'WebFileDownloader' is not defined'
-
Re: Download Files From Web With Progressbar
did you actually add the WebFileDownloader.vb class file to your project?
-
Re: Download Files From Web With Progressbar
O.O No I didn't. I never thought of that. Sorry, I'm new to .NET and don't understand some things yet.
-
Re: Download Files From Web With Progressbar
No problem, but that is what you need to do. Once you add that class file to your project, you can access that class. ;)
-
Re: Download Files From Web With Progressbar
Quote:
Originally Posted by RingOfFire
Thank you for the sample project.
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!
-
Re: Download Files From Web With Progressbar
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?
-
Re: Download Files From Web With Progressbar
Quote:
Originally Posted by kleinma
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).
-
Re: Download Files From Web With Progressbar
are you sure it isn't just that it appears to be slower?
Remember the progress will look a lot faster when its just showing kilobytes downloaded versus megabytes...
The reason I say this, is because when progress of the download gets reported, its only in bytes, not in KB or MB.
The number of bytes is passed to the shared FormatFileSize method, which returns the correct display string (ie kb, mb, etc..)
so why don't you try NOT using the FormatFileSize, and see if you notice slowdown, I sure don't.
If you are using my exact sample project, in the _Downloader.AmountDownloadedChanged event, comment the line
Code:
lblProgress.Text = WebFileDownloader.FormatFileSize(iNewProgress) & " of " & WebFileDownloader.FormatFileSize(ProgBar.Maximum) & " downloaded"
and add the line
Code:
lblProgress.Text = iNewProgress.ToString & " of " & ProgBar.Maximum.ToString & " downloaded"
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.
-
Re: Download Files From Web With Progressbar
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
-
Re: Download Files From Web With Progressbar
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.
edit2:
This also works (1 MB/s):
Code:
lblProgress.Text = "Bestandsgrootte: " & WebFileDownloader.FormatFileSize(Progbar.Maximum)
-
Re: Download Files From Web With Progressbar
well that is very common no matter what...
any UI updating ALWAYS slows an app 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.
-
Re: Download Files From Web With Progressbar
Quote:
Originally Posted by kleinma
well that is very common no matter what...
any UI updating ALWAYS slows an app 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.
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).
-
Re: Download Files From Web With Progressbar
Quote:
Originally Posted by kleinma
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!
-
Re: Download Files From Web With Progressbar
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
-
Re: Download Files From Web With Progressbar
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
-
Re: Download Files From Web With Progressbar
A simple way would be this:
http://203.199.49.90/XMLXX/CCnn/15KT...serid=sam&pwd=[encrypted password here]
-
Re: Download Files From Web With Progressbar
Quote:
Originally Posted by sameer spitfire
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")
-
Re: Download Files From Web With Progressbar
Nice job but how can i make it auto-update when start and how can I let it automaticly extract the rar
-
Re: Download Files From Web With Progressbar
Those questions are beyond the scope of what this example does. This example code is specifically just for downloading files with progress.
You should really ask your questions in the main VB.NET forum.
-
Re: Download Files From Web With Progressbar
-
Re: Download Files From Web With Progressbar
Nice sample but how can i add authentical for rapidshare.com. Thx
-
Re: Download Files From Web With Progressbar
Quote:
Originally Posted by michalss
Nice sample but how can i add authentical for rapidshare.com. Thx
In the downloader class, in the routine called DownloadFileWithProgress you could add the following line of code:
Code:
wRemote.Credentials = New Net.NetworkCredential("USERNAME", "PASSWORD")
this should go right after
Code:
wRemote = WebRequest.Create(URL)
and before
Code:
Dim myWebResponse As WebResponse = wRemote.GetResponse
-
Re: Download Files From Web With Progressbar
Quote:
Originally Posted by kleinma
In the downloader class, in the routine called DownloadFileWithProgress you could add the following line of code:
Code:
wRemote.Credentials = New Net.NetworkCredential("USERNAME", "PASSWORD")
this should go right after
Code:
wRemote = WebRequest.Create(URL)
and before
Code:
Dim myWebResponse As WebResponse = wRemote.GetResponse
Im sorry for my question but can you give me more info or simple exaple appl. Thx i will be more then greatfull...
EDIT : I Have Try your exaple and i was add this syntaxe but it did not work...
-
Re: Download Files From Web With Progressbar
what more do you need?
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
-
Re: Download Files From Web With Progressbar
Quote:
Originally Posted by kleinma
what more do you need?
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
Can you check it for me pls?
EDIT : Its download only 5 Kb from 100 MB....
-
Re: Download Files From Web With Progressbar
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.
-
Re: Download Files From Web With Progressbar
Quote:
Originally Posted by kleinma
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.
Thx for quick response : I have try download http://rs201.rapidshare.com/files/82...9.0.0e-DVT.rar
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
-
Re: Download Files From Web With Progressbar
do you have a premium acct for rapidshare?
-
Re: Download Files From Web With Progressbar
Quote:
Originally Posted by kleinma
do you have a premium acct for rapidshare?
Yes i have 2 if you need ...
-
Re: Download Files From Web With Progressbar
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.
-
Re: Download Files From Web With Progressbar
Here is a link to an article from Karl Moore, that can determine if a connection to the internet is available.
http://www.developerfusion.co.uk/show/3903/
-
Re: Download Files From Web With Progressbar
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
-
Re: Download Files From Web With Progressbar
hello people, this is my first post...
First, best regards for kleinma, this is what i need for my project for school,
i know that old thread but must try...
i need to figure the whole core so i can adjust it and put in my project...
here is a question:
why i need this ??? it is a property ok, but in whe class it only use first line mCurrentFille, i didnt see the roll of property... i have remove it and it still works..... so why ? :)))
Code:
Private mCurrentFile As String = String.Empty
Public ReadOnly Property CurrentFile() As String
Get
Return mCurrentFile
End Get
End Property
back to work... more questions to come :)))
i need to figure it out how to fix bug with "after close it still downloads"
also for my project i need to download list of url's......... my app generates web urls and i need to download it all..... i need to figure it out how... maybe some loop...
great work !!!
-
Re: Download Files From Web With Progressbar
It is really just a helper property incase you want to access the name of the file being downloaded by the downloader class, from the code that calls the download.
So in the example app, there is code in the form that handles the _Downloader.FileDownloadComplete event.
It looks like this:
Code:
'FIRES WHEN DOWNLOAD IS COMPLETE
Private Sub _Downloader_FileDownloadComplete() Handles _Downloader.FileDownloadComplete
ProgBar.Value = ProgBar.Maximum
MessageBox.Show("File Download Complete")
End Sub
However you could do something like this using that property:
Code:
'FIRES WHEN DOWNLOAD IS COMPLETE
Private Sub _Downloader_FileDownloadComplete() Handles _Downloader.FileDownloadComplete
ProgBar.Value = ProgBar.Maximum
MessageBox.Show("File Download for " & _Downloader.CurrentFile & " Complete")
End Sub
Another option would be making a custom eventargs to pass to that event that includes the filename of the downloaded file instead, I just didn't make it that robust because I did not have a need to for the app I coded this for originally.
-
Re: Download Files From Web With Progressbar
True True, thx for fast reply...
i have put some check for URL that user type in...
if user put http:// or if not, app will put...
before that app report error... and user must type WHOLE url... and we know how users are :))))))))))
Code:
in
Private Sub cmdDownload_Click
If Mid(txtURL.Text, 1, 7) <> "http://" Then
Dim temp, http As String
temp = txtURL.Text
http = "http://"
txtURL.Text = http & temp
End If
------------- continue core
'Download
Try
_Downloader = New WebFileDownloader
_Downloader.DownloadFileWithProgress(txtURL.Text, txtDownloadTo.Text.TrimEnd("\"c) & GetFileNameFromURL(txtURL.Text))
Catch ex As Exception
MessageBox.Show("Error: " & ex.Message)
End Try
End Sub
but i have one problem:
i have form1, and on form1 i have button, when click form2 pop up...
but if i close form1 form2 also will be closed... how can i make form2 stay?
AND what is key word for checking IS FORM2 ACTIVE ? of IS FORM2 opened?
thx
SxOne
-
Re: Download Files From Web With Progressbar
What version of Visual Studio are you using?
In 2005 and above, there is a setting in project properties which says "Shutdown Mode" and it is set to "When Startup Form Closes" by default.
So if Form1 is your startup form, then closing it tells you app the whole thing should close. Change this value to "when last form closes" and that should solve your issue.
Form.ActiveForm will return the active form anywhere inside your application. It is a shared property so no instance is required to use it. It can however return a value of Nothing, in the event there is no active form.
-
Re: Download Files From Web With Progressbar
hehe i see what shutdown option in 2005 studio, but for this project i write in 2003 :((((((((((( shame, i have converted to 2005 but design of app. is not like it need to be :(
Code:
Me.ShutDownStyle = Global.Microsoft.VisualBasic.ApplicationServices.ShutdownMode.AfterAllFormsClose
ok for now i will use form2.showDialog() so thats ok for now :)
thx for Form.ActiveForm but i think that i cant check if form2 is open, because if i try to close form1 and in "closing" event of form1 i put Form.ActiveForm Form1 will be active :) so must be something else.. but not important i will escape from that...
thx Kleinma ;)
-
Re: Download Files From Web With Progressbar
hey do you have some idea how can i download more then one file from web... some loop
actually let me explain...
i have app that generates list of url's
after that a need to download it all....
i have an idea that maybe its best to put them in .txt file and forward it to your Download app.....
i need to figure it how...
out to work on it :)
-
Re: Download Files From Web With Progressbar
I guess it depends on if you want to download one file at a time, or all of them at once. It was not really designed to perform multiple async downloads at the same time, however if you are just talking about downloading multiple files, one after another, then it could be as simple as making a list of files to download, and in the download complete event that fires off when a download has completed, you simply kick off the call to download the next file, until all files have been downloaded.
-
Re: Download Files From Web With Progressbar
yes, i mean one file at a time, not multiple...
thanks for idea, i will try that !!!
-
Re: Download Files From Web With Progressbar
Quote:
Originally Posted by sdk1985
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
Hmm, i have converter my project from .net 2003 to 2005 and i also get this Warning, weird, but there is return :confused:
also i get warning abour DialogResult.no, yes, or whatever is used...
i have resolved this with
Code:
System.Windows.Forms.DialogResult....
EDIT:
ok i have resolved FormatFileSize Warning Case else is missing:
Code:
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"
Case Else
Return Size.ToString & "bytes"
End Select
Matt can you explain this please ("D" & "N" is format right):
Size.ToString("D") .ToString("N")
also cant find help about URL.IndexOf("/"c) and URL.LastIndexOf("/"c)
what is c for ? (description is Char, but please explain :)
thx in advance !
-
Re: Download Files From Web With Progressbar
Quote:
Originally Posted by S-One
Hmm, i have converter my project from .net 2003 to 2005 and i also get this Warning, weird, but there is return :confused:
also i get warning abour DialogResult.no, yes, or whatever is used...
i have resolved this with
Code:
System.Windows.Forms.DialogResult....
EDIT:
ok i have resolved
FormatFileSize Warning
Case else is missing:
Code:
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"
Case Else
Return Size.ToString & "bytes"
End Select
Matt can you explain this please ("D" & "N" is format right):
Size.ToString("D") .ToString("N")
also cant find help about URL.IndexOf("/"c) and URL.LastIndexOf("/"c)
what is c for ? (description is Char, but please explain :)
thx in advance !
Yeah this project was originally done in VS2003 (.NET 1.1) they added in some warnings in the newer versions of VS, so that is what you were seeing.
Those fixes you made were fine.
"D" is for decimal formatting of a number as a string, and "N" is for general number formatting of a number as a string. In some cases, they may yield the same result, and in others they will not. (usually has to do with number of decimal place precision, etc..)
Here is a listing of valid numeric formats for calling ToString
http://msdn.microsoft.com/en-us/libr...9k(VS.80).aspx
The IndexOf method is a method that simply returns the numeric index of where a given character is in a string of characters.
So given the string "abccba", the character positions of the letters are as follows:
a = 0
b = 1
c = 2
c = 3
b = 4
a = 5
So if I were to get the indexof("a"c), it will return 0, as a is found at position 0 in this string, however getting lastindexof("a") does the same thing, but starts at the end of the string, in which case it will find a in position 5, so it will return a 5.
the c after the specified character is just to tell the compiler "this is a datatype char, not a string with only 1 character". By default VB thinks anything in quotes is a string.
-
Re: Download Files From Web With Progressbar
thanks Matt !!!
great answer !!!
-
Re: Download Files From Web With Progressbar
Hey Matt, can i ask for one more thing, can you help me how to delete file that didnt whole downloaded, i made Cancel button but cant figure it out where to put delete method, and which is a delete method...
is this enough to Flush all buffers from memory or need something more ?
Code:
If Cancel = True Then
sChunks.Close()
FS.Close()
FS = Nothing
RaiseEvent CancelResetLabelText()
Return False
End If
this is in "DownloadFileWithProgress" Do While Loop
i need this for school project so if you can please help me...
-
Re: Download Files From Web With Progressbar
i have resolved problem with deleting non downloaded file.
In "DownloadFileWithProgress" function in Do While Loop, i have added Cancel option before and in that part i have added this line:
My.Computer.FileSystem.DeleteFile(Location.ToString)
[code]
Do
If Cancel = True Then
sChunks.Close()
FS.Close()
FS = Nothing
My.Computer.FileSystem.DeleteFile(Location.ToString)
RaiseEvent CancelResetLabelText()
Return False
End If
.................
[code]
it works great file is deleted if it's canceled, so now my project is finished... i will post Screenshots later...
thanks again matt for Help, and fot this application !!!!!!!!!!!
Best Regards,
ShOne from Serbia
-
Re: Download Files From Web With Progressbar
superb script.
Quick question, is it possible to modify to download a web folders contents as opposed to a file???
cheers :)
-
Re: Download Files From Web With Progressbar
it downloads the file over HTTP, so if you knew the specific file names, then of course you could run it in a loop and download each file.
If you mean can you point it to a folder location on the web, and have it download every file without knowing their names, then no. A webserver doesn't serve up the list of files in a given folder unless directory browsing is on. If directory browsing was on, you could in theory navigate to the directory, parse the list of files, and then download them individually because you would now have their names.
If you were to go a different route, like FTP if it were possible for you, then you can do things like list the contents of a directory without downloading anything, and then download the entire directories contents.
-
Re: Download Files From Web With Progressbar
wow that was quick :eek:
I have director browsing installed and I only want to use it for small files but many of them.
I'm new so please be gently :D
What do I need to do to get the directory downloaded
again many thanks :)
-
Re: Download Files From Web With Progressbar
you would want to download the HTML that is output by your webserver when you visit that page, and parse it to get the file names. Once you have each individual file name in a collection or an array, then you could use my code in this thread to loop and download each file.
You should really start a new thread in the VB.NET section of this forum, asking about downloading and parsing a file list from a website with directory browsing on.
-
Re: Download Files From Web With Progressbar
Matt,
Will do thanks for directing me in the right direction.
Your script is a real diamond
-
Re: Download Files From Web With Progressbar
Check back soon, I am working on a new version which has a few extra features.
-
Re: Download Files From Web With Progressbar
Hey Matt, me again
Can you explain to me why did you wrote this line ?
Dim myWebResponse As WebResponse = myWebRequest.GetResponse
i understand first part, but why did you initializes it ?? and what did you get with it ?
also this line same question
Dim myStream As Stream = myWebResponse.GetResponseStream
thx in advance...
Sxone