.Net ramblings
# Friday, 17 February 2006
FtpWebRequest with ProgressBar [C# Ftp]
Screenshot of upload operationhi,
i was delighted to see .Net 2.0 has an excellent class for working with FTP.  looking back i can't believe it wasn't part of 1.1. 

i noticed there was no built-in support for reporting the progress of an FTP operation to a user interface.  I needed this functionality in an app i'm working on so i built a class and i'm posting it here in case anyone is interested.

The class FtpProgress inherits from BackgroundWorker, so you just drag an FtpProgress component onto your form, hook up events for Completed and ProgressChanged, and your laughing.  There is a mini class called FtpSettings which stores all the connect info etc to pass in as a paramter to the RunWorkerAsync() method.

I have a variable called 'ChunkSize' which determines the size of each buffer written to the FtpWebRequest output stream.  It is set to 4k by default, and you can increase this if you want for very fast networks but i don't think it will make much difference.  I think the overhead is negligible.

Download the source code and demo application [30 k].

It only does upload, so if you want download functionality, you can do the mirror-image of the upload code, i.e. instead of writing buffers to the output stream, read it from the input stream, or however that would work :) 

here is all the code you need to start an Upload, and report progress:
private void btnUpload_Click(object sender, EventArgs e)
// create a new FtpSettings class to store all the paramaters for the FtpProgress thread
FtpSettings f = new FtpSettings();
f.Host = this.txtHost.Text;
f.Username = this.txtUsername.Text;
f.Password = this.txtPassword.Text;
f.TargetFolder = this.txtDir.Text;
f.SourceFile = this.txtUploadFile.Text;
f.Passive = this.chkPassive.Checked;
f.Port = Int32.Parse(this.txtPort.Text);

private void ftpProgress1_ProgressChanged(object sender, ProgressChangedEventArgs e)
this.toolStripStatusLabel1.Text = e.UserState.ToString(); // the message will be something like: 45 Kb / 102.12 Mb
this.toolStripProgressBar1.Value = Math.Min(this.toolStripProgressBar1.Maximum, e.ProgressPercentage);

Friday, 17 February 2006 18:09:05 (GMT Standard Time, UTC+00:00)  #    Comments [29]  .Net General | .Net Windows FormsTracked by:
"flagyl side effects how long" (flagyl side effects how long) [Trackback]
"scuola lingua" (scuola lingua) [Trackback]
"birdhouse distributors" (birdhouse distributors) [Trackback]
"asiatiche merda nella stanza" (asiatiche merda nella stanza) [Trackback]
"palm scheduler appointments" (palm scheduler appointments) [Trackback]
"business cards" (business cards) [Trackback]
"bed and breakfast positano" (bed and breakfast positano) [Trackback]
"attivita commerciale" (attivita commerciale) [Trackback]
"blue cross of ca" (blue cross of ca) [Trackback]
"mouse pad" (mouse pad) [Trackback]
"I Want to Play the Price Is Right" (I Want to Play the Price Is Right) [Trackback]
"plants retail frisco%2c tx" (plants retail frisco%2c tx) [Trackback]
"street racing videos" (street racing videos) [Trackback]
"limousine stripper" (limousine stripper) [Trackback]
"altro consumo it" (altro consumo it) [Trackback]
"lake compounce" (lake compounce) [Trackback]
"amore segreto" (amore segreto) [Trackback]
"amateur adult mpeg archives" (amateur adult mpeg archives) [Trackback]
"printing magnetic business cards" (printing magnetic business cards) [Trackback]
"girls gone wild" (girls gone wild) [Trackback]
"volagratis" (volagratis) [Trackback]
"song lyrics riding dirty" (song lyrics riding dirty) [Trackback]
"handsome teen scopata" (handsome teen scopata) [Trackback]
"fast weight loss wellbutrin" (fast weight loss wellbutrin) [Trackback]
"ridicolo fighetta fotti" (ridicolo fighetta fotti) [Trackback]
"free teen sex video" (free teen sex video) [Trackback]
"citizen wrist watch" (citizen wrist watch) [Trackback]
"ragazza ginevra" (ragazza ginevra) [Trackback]
"raven" (raven) [Trackback]
"cpa client newsletters" (cpa client newsletters) [Trackback]

Friday, 12 May 2006 09:20:23 (GMT Daylight Time, UTC+01:00)
Excellent! Thanks for posting this code.
Wednesday, 28 June 2006 17:53:58 (GMT Daylight Time, UTC+01:00)
Love your code !!!
Many thanks.
Hebin Wei
Monday, 21 August 2006 21:49:12 (GMT Daylight Time, UTC+01:00)
Just found this and the example looks excellent. When i open the project it complains that it cant find the progress bar.
Do i need to "install" it in some way. Sorry for a newb question but i am a newb.
Richard Warriner
Monday, 21 August 2006 23:52:06 (GMT Daylight Time, UTC+01:00)
Hi Richard,
I tried downloading the sample code and I found the same thing. I re-built the solution in visual studio, and then opened the Form again, and it worked. I think the visual studio designer can't display components unless they have been built. My fault for not including all the binaries to make it work first time.

Thanks for pointing this out!
Good luck
Monday, 07 May 2007 15:11:42 (GMT Daylight Time, UTC+01:00)
Hi! Great Code!

How I Can get the name of the file uploaded in RunWorkerCompleted Event?
Gileno Oliveira
Monday, 07 May 2007 16:08:21 (GMT Daylight Time, UTC+01:00)
Hi Gileno,
It will be the same name as the file you uploaded. The server will not rename the file. you can pass in the name of the file to the RunWorkerAsync() event and then you can forward the same value with the return value of the backgroundworker. The class library explains it very well.
Good luck
Friday, 22 June 2007 21:32:55 (GMT Daylight Time, UTC+01:00)
Hello! This post has been a lifesaver. However, I have a question. I've been rebuilding this example in VB.net, and everything's great...except I can't figure out how to add the FTPProgress component to the toolbox so that I can drag it onto my form.
Ashley Myers
Saturday, 23 June 2007 12:11:59 (GMT Daylight Time, UTC+01:00)
hi ashley
Msdn2 has great instructions on using the VS toolbox:

hope this helps
Sunday, 27 July 2008 15:50:46 (GMT Daylight Time, UTC+01:00)
how to upload multiple files using your example?
Monday, 28 July 2008 09:17:46 (GMT Daylight Time, UTC+01:00)
hi bert. just create an FtpProgress object for each transfer. then you can either start them all asynchronously at the same time, or you can queue them up and wait for each one to complete one at a time. i'll leave the implementation up to you because there are a hundred ways you can do it.
good luck
tim mackey
Thursday, 09 October 2008 08:51:06 (GMT Daylight Time, UTC+01:00)
I implemented a multi uploader from your solution. but there is new exception. ftpprogress gives timeout sometime. how can I handle this nicely ?

Thursday, 09 October 2008 09:38:21 (GMT Daylight Time, UTC+01:00)
hi. there is a thread on this topic over on MSDN, the problem is explained but not resolved satisfactorily.
hope this helps
tim mackey
Thursday, 09 October 2008 13:39:24 (GMT Daylight Time, UTC+01:00)
I solved issue like this:
private void FtpProgress_DoWork(object sender, DoWorkEventArgs e)
while (true)
UploadFiles(sender, e);

I think its not the best way but it will try till upload everything thats is enough for me anyway :)
thanks for the code
Friday, 09 January 2009 07:17:38 (GMT Standard Time, UTC+00:00)
I'm have a real problem with your code, tried it compiled in VS2005 and VS2008. I would really appreciate some help with as I love your work here. Here is the error I get, EVERYTIME at 21.79 MB through a text file.

System.Exception was unhandled by user code
Message="Error occurred during upload, too many retries. \nSystem.IO.IOException: Unable to write data to the transport connection: An established connection was aborted by the software in your host machine. ---> System.Net.Sockets.SocketException: An established connection was aborted by the software in your host machine\r\n at System.Net.Sockets.Socket.Send(Byte[] buffer, Int32 offset, Int32 size, SocketFlags socketFlags)\r\n at System.Net.Sockets.NetworkStream.Write(Byte[] buffer, Int32 offset, Int32 size)\r\n --- End of inner exception stack trace ---\r\n at System.Net.Sockets.NetworkStream.Write(Byte[] buffer, Int32 offset, Int32 size)\r\n at System.Net.FtpDataStream.Write(Byte[] buffer, Int32 offset, Int32 size)\r\n at FTP_ProgressBar.FtpProgress.FtpProgress_DoWork(Object sender, DoWorkEventArgs e) in C:\\Documents and Settings\\awilliams\\Desktop\\FTP_ProgressBar(2)\\FTP_ProgressBar\\FtpProgress.cs:line 64"
at FTP_ProgressBar.FtpProgress.FtpProgress_DoWork(Object sender, DoWorkEventArgs e) in C:\Documents and Settings\awilliams\Desktop\FTP_ProgressBar(2)\FTP_ProgressBar\FtpProgress.cs:line 83
at System.ComponentModel.BackgroundWorker.OnDoWork(DoWorkEventArgs e)
at System.ComponentModel.BackgroundWorker.WorkerThreadStart(Object argument)

Again thank you, I would really appreciate some help.
Tuesday, 22 September 2009 14:18:29 (GMT Daylight Time, UTC+01:00)
so how should i resume the broken upload,shall i write a appln on the server side,or i should write a appln on remoting and send the same data using a port,which one is a better soln
Tuesday, 22 September 2009 15:26:42 (GMT Daylight Time, UTC+01:00)
hi karthik, the code has a built-in 'retry' feature, no need to write any other code, unless you want to be able to resume an upload after quitting the application (and the retry feature has reached its maximum). i would suggest you could modify the client app to start uploading at a specific offset, which could be retrieved by querying the size of a partially uploaded file on the server (web service?).
good luck
Wednesday, 30 September 2009 04:47:45 (GMT Daylight Time, UTC+01:00)
hi tim,
it works ,but after quitting the program(client side) which is currently running,and start a new one.if
i quit my program on the client side the file i uploaded on the ftpserver machine got disappeared so its not possible for me to continue the upload further,so i must start it from the beginning itself.so evry time i close the exe and start it afresh and achieve my task,is it possible to upload without starting the exe again .why the file got disappeared when i close the exe in the client side.waiting for ur response.

Wednesday, 30 September 2009 09:13:33 (GMT Daylight Time, UTC+01:00)
hi karthik. the behaviour you have found is due to the internal design of the FtpWebRequest class and nothing i have control over. you could ask your question on a newsgroup as to why the class is designed that way.
best of luck
Friday, 15 January 2010 04:10:38 (GMT Standard Time, UTC+00:00)
Hi, Tim:

This comment is from your class
// send this chunk to the server. it is sent as a byte[] parameter, but the client and server have been configured to encode byte[] using MTOM.

How is the client and server configured for MTOM, is this in code, app.config?
Friday, 15 January 2010 16:57:13 (GMT Standard Time, UTC+00:00)
hi eric. that was unintentional, thanks for pointing it out. i borrowed some of the code from an MTOM project i had been working on. it doesn't have to use MTOM, but you can turn it on if you want, via app.config and web.config. google MTOM or look up MSDN2 docs for full info.
best of luck
Thursday, 04 February 2010 17:23:00 (GMT Standard Time, UTC+00:00)

When I delete ftpProgress1 and then add another FtpProgress Object with name ftpProgress1

the progress bar is not working. Would you tell please tell me the reason for this. The reason why i'm asking is;
i have copied you files FtpProgress.cs , FtpProgress.Designer.cs and FtpProgress.resx into my project and i have copied all the necessary code also but
i cannot see any change in the progress bar during upload.

Thursday, 04 February 2010 17:34:48 (GMT Standard Time, UTC+00:00)
hi scarface. i would suggest studying the demo application and make sure that it works and that you understand how it works. then when you reproduce in your own app, if it doesn't work it's because you're doing something different than the demo app, i can't help you there. best of luck.
Monday, 02 August 2010 22:13:19 (GMT Daylight Time, UTC+01:00)

Could you point me to the right place to look for chmoding the file after is it uploaded?
Thursday, 14 October 2010 22:53:56 (GMT Daylight Time, UTC+01:00)
Hello Tim,
Thanks for the post its very useful. i got it working. i would like to use the same application from a share point page. do you have any idea of how this can be done on a share point page. it would be grateful if i get any guidance on implementing this as a asp.net web site page. please give me some idea.
Wednesday, 03 November 2010 13:46:33 (GMT Standard Time, UTC+00:00)
Tank you, man.
I'm going crazy with this.
Monday, 10 January 2011 08:44:00 (GMT Standard Time, UTC+00:00)
If anyone adapts this for downloading, please let me know.
It is a bit above my level of understanding how to do that for the progress bar object.
Sunday, 10 July 2011 07:43:39 (GMT Daylight Time, UTC+01:00)
Thanks a lot.
Monday, 03 October 2011 20:51:03 (GMT Daylight Time, UTC+01:00)
Hi Tim,

I have test you code very intensive. There is one Problem.
When the Backgroundworker start, your Upload Button change to Cancel.
When I want cancel the upload it skip the code and go to else:

else if (e.Cancelled)
this.toolStripStatusLabel1.Text = ........
this.toolStripStatusLabel1.Text = ........

Have you a solution for it?

Greets Tony
Thursday, 01 December 2011 12:58:09 (GMT Standard Time, UTC+00:00)
Thanks good work!
Please login with either your OpenID above, or your details below.
Home page

Comment (Some html is allowed: a@href@title, strike) where the @ means "attribute." For example, you can use <a href="" title=""> or <blockquote cite="Scott">.  

[Captcha]Enter the code shown (prevents robots):

Live Comment Preview