Custom Search
Logiclabz
  • Home
  • C#
  • FTP File Upload using FtpWebRequest in .Net C#

FTP File Upload using FtpWebRequest in .Net C#

FtpWebRequest Class in System.Net namespace helps in implementing a File Transfer Protocol (FTP) client in .Net.
To obtain an instance of FtpWebRequest, use the Create method.
The URI is of the form "ftp://127.0.0.1/path", first the .NET Framework logs into the FTP server using the user name and password set by the Credentials property, then the current directory is set to <UserDirectory>/path.
FtpWebRequest.KeepAlive specifies whether the control connection to the FTP server is closed after the request completes.
FtpWebRequest.UseBinary specifies the data type for file transfers.
FtpWebRequest.Method sets the command to send to the FTP server.
WebRequestMethods.Ftp.UploadFile Field represents the FTP STOR protocol method that uploads a file to an FTP server.
When using an FtpWebRequest object to upload a file to a server, you must write the file content to the request stream obtained by calling the GetRequestStream method.
You must write to the stream and close the stream before sending the request.

Namespace used:

using System.Net;
using System.IO;

Sample Code:
    public void ftpfile(string ftpfilepath, string inputfilepath)
    {
        string ftphost = "127.0.0.1";
        //here correct hostname or IP of the ftp server to be given

        string ftpfullpath = "ftp://" + ftphost + ftpfilepath;
        FtpWebRequest ftp = (FtpWebRequest)FtpWebRequest.Create(ftpfullpath);
        ftp.Credentials = new NetworkCredential("userid", "password");
        //userid and password for the ftp server to given

        ftp.KeepAlive = true;
        ftp.UseBinary = true;
        ftp.Method = WebRequestMethods.Ftp.UploadFile;
        FileStream fs = File.OpenRead(inputfilepath);
        byte[] buffer = new byte[fs.Length];
        fs.Read(buffer, 0, buffer.Length);
        fs.Close();
        Stream ftpstream = ftp.GetRequestStream();
        ftpstream.Write(buffer, 0, buffer.Length);
        ftpstream.Close();
    }

Function can be used as
ftpfile(@"/testfolder/testfile.xml", @"c:\testfile.xml");


Comments

  • Tim says:
    Jul 28, 09

    Really useful piece of code. Thanks for this!

  • Muzi says:
    Dec 10, 09

    Thanks great article! I have a problem uploading big files, could kindly suggest a method for uploading big file and different file types? thanks in advance

  • jashwant says:
    Feb 13, 10

    I am using Fileupload (asp.net 3.5) I do not want to give 'inputfilepath' deliberately. Is there any method, so that the file selected from FileUpload can be sent via ftp ??/

  • Edms says:
    Mar 19, 10

    I have a similar code, it works but the file turns into and unrecognised format file. for eg, trial.txt turns into trial any solution to this??

  • Srinivasan says:
    Mar 22, 10

    i get an exception @ below line Stream ftpstream = ftp.GetRequestStream(); saying The remote server returned an error: (501) Syntax error in parameters or arguments. Please let me know the solution for this exception.

  • Larry says:
    Mar 25, 10

    I get "Cannot re-call BeginGetRequestStream/BeginGetResponse while a previous call is still in progress" when I do ftpstream.Close(), anyone got any idea on how I can check if the call is still in progress?

  • Ashish says:
    Jun 23, 10

    Really useful piece of code.

  • Candido says:
    Jun 24, 10

    Excelent code. It really works well in most FTP servers. But in some FTP servers I get a 501 error. Any ideas why? Usually 501 error is due to not implemented errors.

  • GoingPostal says:
    Jun 24, 10

    This code won't work if you sit behind a web proxy ;)

  • piyush says:
    Jul 01, 10

    i get error on below line Stream ftpstream = ftp.GetRequestStream(); saying The remote server returned an error: (550) File unavailable (e.g., file not found, no access).

  • piyush says:
    Jul 01, 10

    coulde u plz give me solution about it!


Leave a reply


Do you like this post?