C Ftp Download File Progress Bar
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:
NickNick2 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.
PatrickPatrickFtp File Download Test
I wanted to leave a comment to the above post but I am too new :(

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.

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