Click here to Skip to main content
15,888,984 members
Articles / Connection
Article

FtpWebRequest

Rate me:
Please Sign up or sign in to vote.
5.00/5 (2 votes)
11 Oct 2013CPOL1 min read 45.7K   3   1
FtpWebRequestThe FtpWebRequest class enables you to programatically create FTP connections to FTP Servers and transfer files.  If you are

This articles was originally at wiki.asp.net but has now been given a new home on CodeProject. Editing rights for this article has been set at Bronze or above, so please go in and edit and update this article to keep it fresh and relevant.

FtpWebRequest

The FtpWebRequest class enables you to programatically create FTP connections to FTP Servers and transfer files.  If you are interested in using the FtpWebRequest class to upload files to a server, here is a code sample:

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];

 

    //will destroy the object immediately after being used

    using (FileStream fr = ff.OpenRead())

    {

        fr.Read(fileContents, 0, Convert.ToInt32(ff.Length));

    }

 

    using (Stream writer = ftpRequest.GetRequestStream())

    {

        writer.Write(fileContents, 0, fileContents.Length);

    }

    //Gets the FtpWebResponse of the uploading operation

    this.ftpResponse = (FtpWebResponse)this.ftpRequest.GetResponse();

    Response.Write(this.ftpResponse.StatusDescription); //Display response

}

catch (WebException webex)

{

    this.Message = webex.ToString();

}

 
 

Links

http://msdn.microsoft.com/en-us/library/system.net.ftpwebrequest.aspx

 
This article was originally posted at http://wiki.asp.net/page.aspx/283/ftpwebrequest

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
United States United States
The ASP.NET Wiki was started by Scott Hanselman in February of 2008. The idea is that folks spend a lot of time trolling the blogs, googlinglive-searching for answers to common "How To" questions. There's piles of fantastic community-created and MSFT-created content out there, but if it's not found by a search engine and the right combination of keywords, it's often lost.

The ASP.NET Wiki articles moved to CodeProject in October 2013 and will live on, loved, protected and updated by the community.
This is a Collaborative Group

754 members

Comments and Discussions

 
QuestionFTPWebRequest Pin
djs831-Aug-21 19:54
djs831-Aug-21 19:54 
Very nice article. Thank you.
In the article you have mentioned File local path (e.g. c:\\Test.txt)
In case of localhost this works fine. But in my case, my application is hosted on remote/cloud server and I am accessing that application from my local machine's browser. From browser when I select file using file selection dialouge; I don't get full file path or content of that selected file. Because my application is hosted on cloud server.
So when I say C:\\Test.txt... In my case it is like Test.txt file should be present on C: of cloud server and not on local machine. So how can I transfer file in this case. Thank you.

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.