Sponsored Ad

Tuesday, January 5, 2010

How to use C# to upload files to FTP, C# File Upload Source Code

FTP File upload through C#

Many applications need the ability to upload and download files via FTP. Even automated processes regularly interact with FTP servers for transferring information. Recognizing this, Microsoft has given developers a straight forward technique to implement this functionality. This document focuses on showing the easy way to take advantage of what Microsoft has provided in the. NET Framework.

Before coming to move files around, put a few things to light:

Key points

  • Each application will require an object attached to its NetworkCredentials Credentials property. This tells the application how to authenticate against the FTP server.
  • The URI provided in order of application must include the name of the file you need to load or unload. For example, if we are downloading the file "data.xml" of abc.ftp.com our ftp://abc.ftp.com/data.xml URI.
  • You need to be familiar with the Stream object. We will use this object, both upload and download files.
  • These can be simple, when I started moving files to / from FTP servers do not understand some of these concepts, so I thought it would be important! Now go to the code.

Steps to follow


    1. Create ftp object
    2. Login to ftp server using userid and password
    3. Set keepalive property true to multiple use of same instance 
    4. Use binary method to upload data
    5. Set action upload or download
    6. Open file in filesteam
    7. Read the file in buffer
    8. close the filesteam
    9. now open ftpsteam
    10.write buffer at ftp
    11.close the ftp stream

here ftpfilepath is full path of file including file name. and in our case its ftp://abc.ftp.com/data.xml

 

Code Snippet
  1. public void ftpUpload(string ftpfilepath, string inputfilepath)
  2.     {
  3.         try
  4.         {
  5.             //create ftp object
  6.             FtpWebRequest ftp = (FtpWebRequest)FtpWebRequest.Create(ftpfilepath);
  7.  
  8.             //  ftp.Credentials = new NetworkCredential("anonymous", "anonymous");
  9.             //userid and password for the ftp server
  10.             ftp.Credentials = new NetworkCredential("ftp_username", "ftp_password");
  11.             //Set keepalive property true to multiple use of same instance  
  12.             ftp.KeepAlive = true;
  13.             //use binary method to upload data
  14.             ftp.UseBinary = true;
  15.             //set action (upload or download
  16.             ftp.Method = WebRequestMethods.Ftp.UploadFile;
  17.             //open file in filesteam
  18.             FileStream fs = File.OpenRead(inputfilepath);
  19.             byte[] buffer = new byte[fs.Length];
  20.             //read the file in filestream
  21.             fs.Read(buffer, 0, buffer.Length);
  22.             //close the filesteam
  23.             fs.Close();
  24.             //now open ftpsteam
  25.             Stream ftpstream = ftp.GetRequestStream();
  26.             //write at ftp
  27.             ftpstream.Write(buffer, 0, buffer.Length);
  28.             //close the ftp stream
  29.             ftpstream.Close();
  30.  
  31.         }
  32.         catch (Exception ex)
  33.         {
  34.             //handle exceptions
  35.             throw;
  36.         }
  37.     }

 

 

How to call function

ftpUpload("ftp://abc.ftp.com/data.xml", Server.MapPath("/abc/Documents/") + "temp.xml");

1 comments:

  1. Really Good Explanation about using FTP with C#
    ReplyDelete

Sponsored Ad

More Related Articles

Website Update

Followers