Click here to Skip to main content
Click here to Skip to main content

Tagged as

Upload file to server using FTP

, 14 Dec 2011 CPOL
Rate this:
Please Sign up or sign in to vote.
You can upload your file to server by writing your own code using FTP details


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


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

using System.IO;
using System.Net;

Then we need to declare four variables as below:

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:

private static void UploadFileToFTP(string source)
                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);
                Stream ftpstream = ftp.GetRequestStream();
                ftpstream.Write(buffer, 0, buffer.Length);
            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.


And we are done...

Points of Interest

I got an opportunity to learn about the below classes:

  • FtpWebRequest
  • WebRequestMethods
  • NetworkCredential


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


    About the Author

    Muthu Nadar
    Team Leader
    India India
    No Biography provided
    Follow on   Twitter   Google+   LinkedIn

    Comments and Discussions

    Questioncan you pls give me a code to transfer to http server PinmemberMember 1134480030-Mar-15 21:14 
    Questionwhy absolutepath? Pinmembermustafakartal3515-Mar-15 9:07 
    GeneralMy vote of 5 PinmemberHumayun Kabir Mamun31-Dec-14 0:20 
    QuestionThe destination filename is invalid PinmemberVedder_18-Feb-14 7:15 
    AnswerRe: The destination filename is invalid [modified] PinprofessionalEd Gadziemski1-Mar-14 8:09 
    AnswerMessage Automatically Removed PinprofessionalRavi.Kumar02113-Feb-14 6:05 
    Questionmy vote of 5 Pinmembersunil134817-Jan-14 0:44 
    GeneralMy vote of 5 Pinmembermendy aaron15-Oct-13 5:25 
    Questionnice stuff PinmemberMember 22005233-Oct-13 4:11 
    QuestionProgress bar Pinmemberahmed rashed24-May-13 23:31 
    GeneralMy vote of 5 Pinmembersachin.vishwa9017-May-13 19:46 
    QuestionException Occured PinmemberMember 85836026-Oct-12 1:06 
    QuestionI wantFTP username and password should be encrypted..How it wiil be done ? Pinmemberasdytfgv1311-Apr-12 21:44 
    For security perpose .
    I wantFTP username and password should be encrypted..How it wiil be done ?
    i dont have FTPS connection
    GeneralReason for my vote of 5 Ok Pinmemberrobinson.netdevelop20-Dec-11 4:55 

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

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

    | Advertise | Privacy | Terms of Use | Mobile
    Web01 | 2.8.150414.1 | Last Updated 15 Dec 2011
    Article Copyright 2011 by Muthu Nadar
    Everything else Copyright © CodeProject, 1999-2015
    Layout: fixed | fluid