Uploading a file via FTP C#

As web developers we all communicate with FTP servers. Desktop applications such as, my personal favourite, FileZilla allow us to manage our files between our PCs and the Server. But the downside is that a user needs to be present to select which files to upload, and to initiate the transfer process.

In many occasions, such as one I faced at work, we need a way of uploading a file to an FTP server without a user having to manually go about doing it. Wouldn’t it be great if we can write into our applications, a method automating the process? Well, the .NET framework allows you to do just that.

The FtpWebRequest class as part of the System.NET namespace, allows us to connect and upload flat files (eg text files, csv files etc) to an FTP server. The example shown below is one usage of the FtpWebRequest class. I put this method in it’s own class so that I could use it at various points throughout my project without having duplicate code.

The class:

using System;
using System;
using System.IO;
using System.Net;
using System.Text;

/// <summary>
/// Simple static class for uploading a file to an FTP server.
/// </summary>
public static class fileUpload
{
    public static string uploadFile(string file)
    {
        // Get the object used to communicate with the server.
        FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://www.mywebserver.com/myfile.txt");
        request.Method = WebRequestMethods.Ftp.UploadFile;

        // This example assumes the FTP site uses anonymous logon.
        request.Credentials = new NetworkCredential("username", "password");

        // Copy the entire contents of the file to the request stream.
        StreamReader sourceStream = new StreamReader(file);
        byte[] fileContents = Encoding.UTF8.GetBytes(sourceStream.ReadToEnd());
        sourceStream.Close();
        request.ContentLength = fileContents.Length;

        // Upload the file stream to the server.
        Stream requestStream = request.GetRequestStream();
        requestStream.Write(fileContents, 0, fileContents.Length);
        requestStream.Close();

        // Get the response from the FTP server.
        FtpWebResponse response = (FtpWebResponse)request.GetResponse();

        // Close the connection = Happy a FTP server.
        response.Close();

        // Return the status of the upload.
        return response.StatusDescription;

    }
}

Basic Usage:

This can be used at any stage in the project without the need of creating a new object, since both the class and the method are defined as static.

fileUpload.uploadFile(myfile.txt);

A better implementation may to be pass the connection and file destination variables as parameters to the uploadFile method, so that the code can be used in a static context with different FTP servers. If anyone would like to know how to achieve this drop a comment below, however this code should get you well on your way 🙂

Source: http://msdn.microsoft.com/en-us/library/ms229715.aspx

  • DizzyBlack

    Nice!

  • Jacob Sebastian

    Thanks mate….