Welcome

Hello, Welcome to my blog. If you like feel free to refer others

Wednesday, 8 April 2015

Upload file into FTP server

Add 2 reference in your class:
using System.Net;
using System.IO;

Then copy below mentioned method

/// <summary>
        /// Upload Design File into FTP server
        /// </summary>
        /// <param name="serverPath"></param>
        /// <param name="localPath"></param>
        /// <param name="fileName"></param>
        /// <param name="settings"></param>
        /// <returns></returns>
        public static bool UploadDesignFile(string serverPath, string localPath, string fileName,Dictionary<string,string> credentials)
        {           
            bool status = false;
          
            FtpWebRequest reqFTP;
            int bufferSize = 2048;
            try
            {
                String uploadUrl = String.Format("{0}{1}/{2}", @"ftp:", serverPath, fileName);
                reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(uploadUrl));
                 string username = string.Empty;
                credentials.TryGetValue("UserId",out username);
                string passwd = string.Empty;
                credentials.TryGetValue("Password",out passwd);
                reqFTP.Credentials = new NetworkCredential(Decrypt(username), Decrypt(passwd));
                reqFTP.UseBinary = true;
                reqFTP.UsePassive = true;
                reqFTP.KeepAlive = true;
                reqFTP.Method = WebRequestMethods.Ftp.UploadFile;
               
                using (Stream uploadStream = reqFTP.GetRequestStream())
                {
                    string localFilePath = String.Format("{0}/{1}", localPath, fileName);
                    using (FileStream localFileStream = File.OpenRead(localFilePath))
                    {
                        /* Buffer for the Downloaded Data */
                        byte[] byteBuffer = new byte[localFileStream.Length];
                        int bytesSent = localFileStream.Read(byteBuffer, 0, bufferSize);
                        /* Upload the File by Sending the Buffered Data Until the Transfer is Complete */
                        try
                        {
                            while (bytesSent != 0)
                            {
                                uploadStream.Write(byteBuffer, 0, bytesSent);
                                bytesSent = localFileStream.Read(byteBuffer, 0, bufferSize);
                            }
                            status = true;
                        }
                        catch (Exception ex)
                        {
                            status = false;
                        }
                    }
                }
               
            }
            catch (Exception ex)
            {
                status = false;
            }
            finally
            {               
                reqFTP = null;
            }
           
            return status;
        }


Above mentioned code will upload your file into FTP server. If file exists it will override that file.

Happy learning.....

1 comment:

  1. God is love! :heart: Catholic blogwalking :-) http://emmanuel959180.blogspot.in/

    ReplyDelete