Click here to Skip to main content
15,867,568 members
Articles / Web Development / ASP.NET
Tip/Trick

Upload file to server using FTP

Rate me:
Please Sign up or sign in to vote.
4.47/5 (32 votes)
14 Dec 2011CPOL1 min read 219.4K   33   22
You can upload your file to server by writing your own code using FTP details

Introduction


This article will explain how to upload your file to server using FTP details.

Background

I am a programmer having intermediate level of experience in .NET development. One fine day, I was asked to create a scheduler program which would create a file and upload that to the server using FTP details. I started developing the code by using StreamWriter class, suddenly I stopped..., thinking of why I am using StreamWriter class as it is just to write a file, and my problem was to upload a file. Then after some R&D, I got the class which helped me to upload the file.


Using the Code


In order to upload a file using FTP details, one should know the server’s FTP URL, FTP username and FTP password.

We can achieve the file uploading task by using the below three inbuilt classes of .NET: FtpWebRequest, WebRequestMethods, and NetworkCredential .



To start with the coding part.

First we need to import the below namespaces


C#
using System.IO;
using System.Net;


Then we need to declare four variables as below:

C#
String sourcefilepath = "@absolutepath"; // e.g. "d:/test.docx"
String ftpurl = "@ftpurl"; // e.g. ftp://serverip/foldername/foldername
String ftpusername = "@ftpusername"; // e.g. username
String ftppassword = "@ftppassword"; // e.g. password


Note: Please replace @absolutepath, @ftpurl, ftpusername, @ftppassword with your actual values.

Copy the below code and paste in your class:


C#
private static void UploadFileToFTP(string source)
        {
            try
            {
                string filename = Path.GetFileName(source);
                string ftpfullpath = ftpurl;
                FtpWebRequest ftp = (FtpWebRequest)FtpWebRequest.Create(ftpfullpath);
                ftp.Credentials = new NetworkCredential(ftpusername, ftppassword);

                ftp.KeepAlive = true;
                ftp.UseBinary = true;
                ftp.Method = WebRequestMethods.Ftp.UploadFile;
                
                FileStream fs = File.OpenRead(source);
                byte[] buffer = new byte[fs.Length];
                fs.Read(buffer, 0, buffer.Length);
                fs.Close();

                Stream ftpstream = ftp.GetRequestStream();
                ftpstream.Write(buffer, 0, buffer.Length);
                ftpstream.Close();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }


The final point will be calling the above procedure by using the below code on whatever event you want the operation to be performed.

UploadFileToFTP(sourcefilepath);

And we are done...

Points of Interest


I got an opportunity to learn about the below classes:

  • FtpWebRequest
  • WebRequestMethods
  • NetworkCredential

    License

    This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


    Written By
    Team Leader
    India India
    This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

    Comments and Discussions

     
    GeneralMy vote of 3 Pin
    sandesh7429-Apr-18 22:14
    sandesh7429-Apr-18 22:14 
    Questionerror: (550) Pin
    sa_hma11-Feb-18 4:13
    sa_hma11-Feb-18 4:13 
    QuestionQuestion1 Pin
    Member 1177976220-Jun-15 2:53
    Member 1177976220-Jun-15 2:53 
    Question[FTP Error Message] The remote server returned an error: (553) File name not allowed Pin
    zidane16824-Apr-15 21:57
    zidane16824-Apr-15 21:57 
    AnswerRe: [FTP Error Message] The remote server returned an error: (553) File name not allowed Pin
    ThomaLuke14-May-17 3:16
    professionalThomaLuke14-May-17 3:16 
    GeneralRe: [FTP Error Message] The remote server returned an error: (553) File name not allowed Pin
    EmilekCz13-Jun-17 3:21
    EmilekCz13-Jun-17 3:21 
    Questioncan you pls give me a code to transfer to http server Pin
    Member 1134480030-Mar-15 20:14
    Member 1134480030-Mar-15 20:14 
    Please provide me a code to upload files to http server.

    Questionwhy absolutepath? Pin
    mustafakartal3515-Mar-15 8:07
    mustafakartal3515-Mar-15 8:07 
    GeneralMy vote of 5 Pin
    Humayun Kabir Mamun30-Dec-14 23:20
    Humayun Kabir Mamun30-Dec-14 23:20 
    QuestionThe destination filename is invalid Pin
    Vedder_18-Feb-14 6:15
    Vedder_18-Feb-14 6:15 
    AnswerRe: The destination filename is invalid Pin
    Ed Gadziemski1-Mar-14 7:09
    professionalEd Gadziemski1-Mar-14 7:09 
    AnswerRe: The destination filename is invalid Pin
    joginder-banger3-Feb-20 17:49
    professionaljoginder-banger3-Feb-20 17:49 
    Questionmy vote of 5 Pin
    sunil134816-Jan-14 23:44
    sunil134816-Jan-14 23:44 
    GeneralMy vote of 5 Pin
    mendy aaron15-Oct-13 4:25
    mendy aaron15-Oct-13 4:25 
    Questionnice stuff Pin
    Member 22005233-Oct-13 3:11
    Member 22005233-Oct-13 3:11 
    QuestionProgress bar Pin
    ahmed rashed24-May-13 22:31
    ahmed rashed24-May-13 22:31 
    GeneralMy vote of 5 Pin
    sachin.vishwa9017-May-13 18:46
    professionalsachin.vishwa9017-May-13 18:46 
    QuestionException Occured Pin
    Member 85836026-Oct-12 0:06
    Member 85836026-Oct-12 0:06 
    QuestionI wantFTP username and password should be encrypted..How it wiil be done ? Pin
    asdytfgv1311-Apr-12 20:44
    asdytfgv1311-Apr-12 20:44 
    AnswerRe: I wantFTP username and password should be encrypted..How it wiil be done ? Pin
    joginder-banger3-Feb-20 17:45
    professionaljoginder-banger3-Feb-20 17:45 
    GeneralReason for my vote of 5 Ok Pin
    robinson.netdevelop20-Dec-11 3:55
    robinson.netdevelop20-Dec-11 3:55 

    General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

    Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.