C Ftp Download File Progress Bar

It converts the progress fraction into a percentage and draws it centered on the control. If you like you can modify the code to change the way it draws the progress bar. For example, you could use different colors, perhaps to. How to download a file in C# (progressbar and download speed) Posted on May 10. Download file from FTP. How to upload a file in windows application and also i have to show how much it is upload in progress bar, please help me out. BW says: February 2, 2015 at 10:52 pm.

22 Apr 2009CPOL
How to download files in .NET with all information about the progess (progressbar, download speed), supports cancel and resume

Introduction

I'll explain how you can build a complete, reliable and powerful download manager with .NET Framework.
It is able to show a rarity of data about the download in progress, including the download speed, the file size, the downloaded size, and the percentage of completion.

A completely new class has been written based on this code, which features logic/layout abstraction, multiple file download support, and is more easy to use by providing a variety of properties and events. Check out the FileDownloader article.

The files are downloaded via the HttpWebRequest and HttpWebResponse, and written to a file with the StreamWriter. All this is done on a separate thread (with a BackgroundWorker), so the application using this downloader won't freeze.

Using the Code

The demo application is pretty simple, and contains only the needed UI components for downloading a file. You can of course implement this downloader in your own application, or just use it to download in the background without any user interaction.

The most important code can be found in the DoWork method of the BackgroundWorker. First, an attempt will be made to establish a connection to the file. If this is successful, a script in the Do loop will start downloading the file block by block, until there are no remaining bytes to be downloaded. Note that as soon as a block of data has been received, it's written to the local target file.

This code uses delegates to invoke methods on the main thread, and pass data about the download progress to them.

The code does not support FTP downloads, but can be easily modified to do this.

Resume Downloads

It is possible to modify the code to allow resuming downloads. Add this code before the first use of the HttpWebRequest object.

Bar

You'll also need to set the Position property of the FileStream instance to the position where you want to resume the download. So be sure you also save this before the download is cancelled.

Calculating Speed

The code contains built in download speed calculation, using the StopWatch class. This will return another result every time 5 packages have been downloaded. Note that you can also manually calculate the speed, and use a timer to get data at a more regular interval.

Active7 years, 4 months ago

I am downloading a file from an FTP site (Async) and need to update a progress bar. I have read MS documentation that states that this can be done is the WebClient class's GetWebRequest() is ovverriden so the 'UsePassive' property is set to 'false'. I have done this but 'DownloadProgressChanged' event argument ProgressPercentage is always '0'.

Can someone tell me how to get this argument to start returning values?

This is the download method:

This is the FTPClient (where i am overriding GetWebRequest()):

And my Callback if it helps:

NickNick
9,99141 gold badges165 silver badges287 bronze badges

2 Answers

UsePassive is used to 'determine' who acts as the server when the connection for the file transfer is made, so it should not have anything to do with the actual status of the transfer. May I ask where you have read this?

Could it be that

  • the change is very small for a large file, so you don't see the percentagechanged?
  • that the file is smaller than ftpwebrequests internal buffer so you get the whole file before any 'update'?

Can you set a breakpoint in UpdateProgress and see anything in any of e's properties?

And as a side note, since you are downloading the file async, another thread is used for the actual download. In your event method you probably want to do something like this:

Which will queue the setmethods to the UI thread instead.

PatrickPatrick

Ftp File Download Test

14.8k5 gold badges60 silver badges77 bronze badges

I wanted to leave a comment to the above post but I am too new :(

Download file from url

Reference to MSDN on overriding the web request method:

However in answer to the OPs question if your FTP server is not set to accept active connections then setting WebClient.UsePassive = false will make no difference.

EDIT: I enabled System.Net.Tracing on my project and tried both passive and active modes and not only do both work as expected... TotalBytes is still -1 so my thinking is the note on MSDN in mistaken or we are missing something

The DownloadFileProgressChangedEventArgs do contain the total bytes received and if you know the file size you can calculate yourself.

There is likely a better way... But I used a quick FtpWebRequest to get the file size and then pass this to the DownloadProgressCallback method to update the progress bar.

Download

Also what the above poster failed to mention is that your update progress method must invoke the control because it is being called by the thread created by DownloadFileAsync and you can only change a control from the thread that created it.

You should use the dispatcher for the control e.g.

See http://msdn.microsoft.com/en-us/library/ms591206.aspx

Dave WilliamsDave Williams

Download File From Url

Not the answer you're looking for? Browse other questions tagged c#ftpwebclient or ask your own question.