+ Reply to Thread
Results 1 to 2 of 2

Thread: Upload file to FTP Server

  1. #1
    Junior Member
    Join Date
    Jan 2012

    Upload file to FTP Server

    I want to upload files via FTP , any idea how can i do that ?
    Last edited by cross; 11-09-2012 at 12:30 AM.

  2. #2
    Junior Member
    Join Date
    Feb 2012
    The parameters of the method will be a file location , web path , username and password
    Those 4 parameters are essentially needed to create a successful FTP Connection.
    Here is the Snippet :
            public void UploadFile(string File_name, string Upload_Path, string Ftp_Username, string Ftp_Password)
                System.IO.FileInfo _FileInfo = new System.IO.FileInfo(File_name);
                // Create FtpWebRequest object from the Uri provided
                System.Net.FtpWebRequest _FtpWebRequest = (System.Net.FtpWebRequest)System.Net.FtpWebRequest.Create(new Uri(Upload_Path));
                // Provide the WebPermission Credintials
                _FtpWebRequest.Credentials = new System.Net.NetworkCredential(Ftp_Username, Ftp_Password);
                // By default KeepAlive is true, where the control connection is not closed
                // after a command is executed.
                _FtpWebRequest.KeepAlive = false;
                // set timeout for 20 seconds
                _FtpWebRequest.Timeout = 20000;
                // Specify the command to be executed.
                _FtpWebRequest.Method = System.Net.WebRequestMethods.Ftp.UploadFile;
                // Specify the data transfer type.
                _FtpWebRequest.UseBinary = true;
                // Notify the server about the size of the uploaded file
                _FtpWebRequest.ContentLength = _FileInfo.Length;
                // The buffer size is set to 2kb
                int buffLength = 2048;
                byte[] buff = new byte[buffLength];
                // Opens a file stream (System.IO.FileStream) to read the file to be uploaded
                System.IO.FileStream _FileStream = _FileInfo.OpenRead();
                    // Stream to which the file to be upload is written
                    System.IO.Stream _Stream = _FtpWebRequest.GetRequestStream();
                    // Read from the file stream 2kb at a time
                    int contentLen = _FileStream.Read(buff, 0, buffLength);
                    // Till Stream content ends
                    while (contentLen != 0)
                        // Write Content from the file stream to the FTP Upload Stream
                        _Stream.Write(buff, 0, contentLen);
                        contentLen = _FileStream.Read(buff, 0, buffLength);
                    // Close the file stream and the Request Stream
                catch (Exception ex)
                    MessageBox.Show(ex.Message, "Upload Error", MessageBoxButtons.OK, MessageBoxIcon.Error);

+ Reply to Thread

Tags for this Thread


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts