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

Upload file to server using FTP

By , 14 Dec 2011
 

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

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)
        {
            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)

    About the Author

    Muthukumar Nadar
    Software Developer Team computers
    India India
    Member
    No Biography provided

    Sign Up to vote   Poor Excellent
    Add a reason or comment to your vote: x
    Votes of 3 or less require a comment

    Comments and Discussions

     
    You must Sign In to use this message board.
    Search this forum  
        Spacing  Noise  Layout  Per page   
    GeneralMy vote of 5membersachin.vishwa9017 May '13 - 18:46 
    this topic is explained so easily. thanx alot bro
    QuestionException OccuredmemberMember 85836026 Oct '12 - 0:06 
    There is exception.
    QuestionI wantFTP username and password should be encrypted..How it wiil be done ?memberasdytfgv1311 Apr '12 - 20: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 Okmemberrobinson.netdevelop20 Dec '11 - 3:55 
    Reason for my vote of 5
    Ok

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

    Permalink | Advertise | Privacy | Mobile
    Web04 | 2.6.130523.1 | Last Updated 15 Dec 2011
    Article Copyright 2011 by Muthukumar Nadar
    Everything else Copyright © CodeProject, 1999-2013
    Terms of Use
    Layout: fixed | fluid