Click here to Skip to main content
12,693,536 members (29,045 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


4 bookmarked

Upload file to server using FTP

, 6 Sep 2013 CPOL
Rate this:
Please Sign up or sign in to vote.
This is an alternative for "Upload file to server using FTP"


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


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 

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.


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

private static void UploadFileOnFtp(IDictionary<string, string> ftpDetails)
               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);

           catch (Exception ex)

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


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


About the Author

Tiwari Shailendra
Software Developer (Senior)
India India
No Biography provided

You may also be interested in...


Comments and Discussions

AnswerMessage Automatically Removed Pin
13-Feb-14 6:09
professionalRavi.Kumar02113-Feb-14 6:09 
QuestionOk start for now Pin
David_Wimbley7-Sep-13 7:50
professionalDavid_Wimbley7-Sep-13 7:50 
GeneralMy vote of 2 Pin
dholakiya ankit6-Sep-13 22:27
professionaldholakiya ankit6-Sep-13 22: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.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170118.1 | Last Updated 7 Sep 2013
Article Copyright 2013 by Tiwari Shailendra
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid