C# Programming > Data

 

Uploading the File

Now for the meat of the program, uploading the file. Doing so is actually simpler than it sounds, all we have to do is get a web stream to the FTP server and write to it like with any other C# IO stream:

Stream reqStream = request.GetRequestStream();
reqStream.Write(buffer, 0, buffer.Length);
reqStream.Close();

The same principle applies to this stream. It is up to you if you want to write the entire buffer to the stream in one line or if you want to have a loop write chunks of 2KB for example, until the entire buffer is written.

For large files it is a good idea to do it in pieces (to avoid the application freezing). Also, if you want to have a progressbar for example, you can update the progressbar after each small chunk is written. Otherwise it is impossible to know how much has been written and how much still has to be uploaded.

Downloading a File

Downloading a file from an FTP server is not all that much different. I will not get into it in much detail here since this is an article on uploading files. But I mention it because the two tasks are usually required within the same project.

In general, downloading files from the internet can be accomplished with technique described in the downloading data in C# article.

If you need to specifically download files from an FTP server (where they might be password protected for example), then you definitely want to look at the FTP file downloading article.

The C# FTP Example

Download the example at the bottom of the page to see it all come together. It's a full-working example of uploading with FTP in C#.

Page 1
< Preparing the File

Back to C# Article List