Click here to Skip to main content
15,880,427 members
Articles / Web Development / ASP.NET
Alternative
Tip/Trick

Upload file to server using FTP

Rate me:
Please Sign up or sign in to vote.
2.67/5 (2 votes)
6 Sep 2013CPOL 18.6K   4   3
This is an alternative for "Upload file to server using FTP"

Introduction

using this article i have demonstrate how to upload text file on ftp server. 

Background

I am programmer having 3+ year of experience in web as well as window development.

Using the code 

upload a file using FTP details, one should know the server’s FTP URL, FTP username and FTP password and specific FTP folder which is hold all file in ftp  

I have set all ftp and file details from database. Code given blow 

C++
IDictionary<string, string> ftpDetails = new Dictionary<string, string>();  
ftpDetails.Add("FTPUrl", dataSet.Tables[2].Rows[0]["FtpUrl"].ToString());
ftpDetails.Add("FTPUserId", dataSet.Tables[2].Rows[0]["FtpUserId"].ToString());
ftpDetails.Add("FTPPassword", dataSet.Tables[2].Rows[0]["FtpPassword"].ToString());
ftpDetails.Add("FTPFolderName", dataSet.Tables[2].Rows[0]["FolderName"].ToString());
ftpDetails.Add("FileName", "FileName"); 
 

All value set in dictonary objects after then call method which is implented the ftp uploading funtionality.

C++
UploadFileOnFtp(ftpDetails);

I have implemented the funtionality for file uploading on ftp server.

C#
private static void UploadFileOnFtp(IDictionary<string, string> ftpDetails)
       {
           try
           {
               FileInfo fileInfo = new FileInfo(ftpDetails["FileName"]);
               string uri = "ftp://" + ftpDetails["FTPUrl"] + "/" + ftpDetails["FTPFolderName"] + "/" + fileInfo.Name;
               FtpWebRequest  ftpWebRequest = (FtpWebRequest)FtpWebRequest.Create(new Uri(uri));
               ftpWebRequest.Credentials = new NetworkCredential(ftpDetails["FTPUserId"], ftpDetails["FTPPassword"]);
               ftpWebRequest.KeepAlive = false;
               ftpWebRequest.Method = WebRequestMethods.Ftp.UploadFile;
               ftpWebRequest.UseBinary = true;
               ftpWebRequest.ContentLength = fileInfo.Length;
               int buffLength = 2048;
               byte[] buff = new byte[buffLength];
               int contentLen;
               FileStream fileStream = fileInfo.OpenRead();
               Stream stream = ftpWebRequest.GetRequestStream();
               contentLen = fileStream.Read(buff, 0, buffLength);
               while (contentLen != 0)
               {
                   stream.Write(buff, 0, contentLen);
                   contentLen = fileStream.Read(buff, 0, buffLength);
               }
               stream.Close();
               fileStream.Close();

           }
           catch (Exception ex)
           {
               LogManager.WriteErrorLog(ex);
               throw;
           }
           finally
           {
               LogManager.WriteTraceLog(trace.ToString());
           }
       }   

The final process whichwill be calling the above procedure by using the below code on whatever event you want the operation to be performed  

License

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


Written By
Software Developer (Senior)
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

 
QuestionOk start for now Pin
David_Wimbley7-Sep-13 6:50
professionalDavid_Wimbley7-Sep-13 6:50 
GeneralMy vote of 2 Pin
Dholakiya Ankit6-Sep-13 21:27
Dholakiya Ankit6-Sep-13 21:27 

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.