C# Programming > Data

Submit a Comment

C# FTP Upload

C# ftp, ftp upload

Why FTP?

Using FTP to upload files in C# is a very simple and universal way to upload files (and download with FTP) from a webserver. Most servers today, free and paid for, have support for FTP uploading. All one needs is a username and password and the operation becomes quite simple.

Luckily the .Net Framework comes with built-in C# FTP upload support, which means no need for external libraries. Note that there are many free FTP dlls to use in C#. Some of them use more complex methods to connect to the server, but many also use this same simple structure.

The way in which the program will be structured, it will be relatively simple to expand its functionality.

Writing the .NET Uploader

The FTP Request

To connect to the FTP server we can use the FtpWebRequest C# object under the System.Net namespace. The object is created with the FtpWebRequest.Create() function. Note that the function will ask for a URL to connect to, the format to write the url is: [FTP Address] + "/" + [Filename of file to upload].

(Be careful not to use "//", double slashing is for "\", this double slash can cause problems with some servers.

Here is the code:

FtpWebRequest request = (FtpWebRequest)FtpWebRequest.Create(FTPAddress + "/" + 
Path.GetFileName(filePath));

As for setting up the object, the two most important settings are the Method and the Credentials settings:

request.Method = WebRequestMethods.Ftp.UploadFile;
request.Credentials = new NetworkCredential(username, password);
request.UsePassive = true;
request.UseBinary = true;
request.KeepAlive = false;

Notice that to link the username and password combination we use the NetworkCredential object. Meanwhile the Method property tells the class we intend to use the FTP connection to upload a file.

Loading the File

The next step would be to transfer the upload file into a byte array which will be used for the FTP upload. A buffer array if you will. Here is a simple method to do it:

FileStream stream = File.OpenRead(filePath);
byte[] buffer = new byte[stream.Length];


stream.Read(buffer, 0, buffer.Length);
stream.Close();

This way is simple since it reads the file all at once. For bigger files to upload it might be a good idea to load them by parts to make sure the program does not freeze. (Use a for loop and stick the link Application.DoEvents() in each iteration).

There is an almost limitless number of ways to read a file in C# so use what you like.

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. To upload a file all at once works for small files, but for bigger files it might be a good idea to do it by parts, 2 Kb parts is a good balance for both small and big files. Additionally with parts one can track the progress of the upload.

Downloading a File

We will not be getting into the downloading part much in this article, but just in general overview, there is two ways to easily download a file off the internet with C#.

If the file is public, which is in many cases the point of uploading it to a FTP server, then you can use the technique in the C# Downloading Data program.

If you prefer to download the file directly from the FTP server, one only needs to tweak the above code. Specifically the Method property and the Write routine.

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# so feel free to test it out...

Download FTP Upload C# Source Code

Back to C# Article List

License Agreement | Privacy Policy
Link to Us | Subscribe to Newsletter | Contact Us