Uploading File using FtpWebRequest

For those people who are interested to use FtpWebRequest to upload files on a server. Here is the code:
 
 
FtpWebRequest ftpRequest;
FtpWebResponse ftpResponse;
    
try
{
    //Settings required to establish a connection with the server
    this.ftpRequest = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://ServerIP/FileName"));
    this.ftpRequest.Method = WebRequestMethods.Ftp.UploadFile;
    this.ftpRequest.Proxy = null;
    this.ftpRequest.UseBinary = true;
    this.ftpRequest.Credentials = new NetworkCredential("UserName", "Password");
    
    //Selection of file to be uploaded
    FileInfo ff = new FileInfo("File Local Path With File Name");//e.g.: c:\\Test.txt
    byte[] fileContents = new byte[ff.Length];
    
    using (FileStream fr = ff.OpenRead()) //will destroy the object immediately after being used
    { 
       fr.Read(fileContents, 0, Convert.ToInt32(ff.Length));
    }
 
    using (Stream writer = ftpRequest.GetRequestStream())
    {
       writer.Write(fileContents, 0, fileContents.Length);
    }
 
    this.ftpResponse = (FtpWebResponse)this.ftpRequest.GetResponse(); //Gets the FtpWebResponse of the uploading operation
    Response.Write(this.ftpResponse.StatusDescription); //Display response
}
catch (WebException webex)
{
   this.Message = webex.ToString();
}

11 Comments

  • I used your code for my project.I can upload file to the FTP from my local developemnent server.
    Once I copied the website to the production server I am having System.IO.FileNotFoundException: Could not find the file...

  • gus : you probably just gave a path as URI string, append a filename and it'll work fine. (I had the same problem as you mention here)

  • Great example code I just tested it and it ran without any issues, thanks for sharing to the programming community.

    Dilmer Valecillos

  • Hi.. i faced the same problem:

    Once I deployed the website to the web server, I am having System.IO.FileNotFoundException: Could not find the file...when i try to upload files.

    However i already gave a path as URI string, append a filename.

    Got any idea?

    thanks in advance

  • Hi,I used the above code but when deployed in the webserver, i'm getting "Could not find the file " error while uploading files.

    Any suggestions on how to resolve this?

    -regards Priya

  • i have the same error file not found but it is working fine in local please help

  • Is the FtpWebRequest available in .Net 2003? I can't find the class. Is not, anyway I can use it .Net 2003. I need to develop the application in .Net 2003 as my mobile device is Pocket PC 2003. Thanks!

  • Because all asp.net code run at server, not in client machine, and the FileStream can read only the files which reside in the same machine where the code run. Read from HttpPostFile instead of your local file, or using Com/Com+ object :)

  • Very Useful Code, Thanks a lot Faraz

  • Thanks

    This really helped me.
    I was using StreamWriter, and I was stuck at how to write the file binary content into it.

  • It helped me a lot!!!Thank you so much...

Comments have been disabled for this content.