Faraz Shah Khan

MCP, MCAD.Net, MCSD.Net, MCTS-Win, MCTS-Web, MCPD-Web

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();
}

Comments

gus said:

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...

# March 25, 2008 1:58 PM

Vincent said:

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)

# November 13, 2008 10:10 PM

Dilmer said:

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

Dilmer Valecillos

# March 26, 2009 10:28 PM

anu said:

.Net ftp upload is found to be time consuming when used in a web application. According to my understanding it may be due to the two way process which occurs ie the http post to the asp.net application and then connecting to the ftp server. Can anyone suggest how to improve ftp performance?

# June 27, 2009 2:02 AM

kathy kee said:

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

# August 3, 2009 12:43 PM

shinipri said:

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

# August 20, 2009 5:10 AM

dhoni said:

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

# November 6, 2009 7:23 AM

Gim Ho said:

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!

# March 25, 2010 9:23 PM

Nguyen Manh Dzung said:

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 :)

# December 14, 2010 11:55 PM

Aboobacker Karippoor said:

Very Useful Code, Thanks a lot Faraz

# January 30, 2012 2:08 AM
Leave a Comment

(required) 

(required) 

(optional)

(required)