Click here to Skip to main content
15,392,078 members
Articles / General Programming / File
Posted 11 Oct 2013


2 bookmarked


Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
11 Oct 2013CPOL1 min read
FtpWebRequest class to upload a file to ftp server (destination )This Function shows how to upload a file ftp server. this function simply return

This articles was originally at but has now been given a new home on CodeProject. Editing rights for this article has been set at Bronze or above, so please go in and edit and update this article to keep it fresh and relevant.

FtpWebRequest class to upload a file to ftp server (destination )

This Function shows how to upload a file ftp server. this function simply return true or false


if uploaded successfully it will return true else false

       public bool Upload(FileInfo fileInfo)
            string uri = "";
            string serverIp = "";
            string Username = "Aamir";
            string Password = "Hasan";

                uri = "ftp://" + serverIp + "/"  + fileInfo.Name;
            FtpWebRequest reqFTP;
            reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(uri));
            reqFTP.Credentials = new NetworkCredential(Username, Password);
            reqFTP.KeepAlive = false;
            reqFTP.Proxy = null;
            reqFTP.Method = WebRequestMethods.Ftp.UploadFile;
            reqFTP.UseBinary = true;
            reqFTP.Timeout = 8000;
            reqFTP.ContentLength = fileInfo.Length;
            int buffLength = 433664;//1048;// *16;
            byte[] buff = new byte[buffLength];
            int contentLen;

            FileStream fs = fileInfo.OpenRead();


                Stream strm = reqFTP.GetRequestStream();
                contentLen = fs.Read(buff, 0, buffLength);
                while (contentLen != 0)
                    strm.Write(buff, 0, contentLen);
                    contentLen = fs.Read(buff, 0, buffLength);

               return true;

            catch (Exception ex)
                if (reqFTP != null)
              return false

This article was originally posted at


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


About the Author

ASP.NET Community
United States United States
The ASP.NET Wiki was started by Scott Hanselman in February of 2008. The idea is that folks spend a lot of time trolling the blogs, googlinglive-searching for answers to common "How To" questions. There's piles of fantastic community-created and MSFT-created content out there, but if it's not found by a search engine and the right combination of keywords, it's often lost.

The ASP.NET Wiki articles moved to CodeProject in October 2013 and will live on, loved, protected and updated by the community.
Group type: Collaborative Group

757 members

Comments and Discussions

-- There are no messages in this forum --