Friday, March 21, 2014

File uploading using FTP in C#

Sometimes we need to upload files into a directory of a different website from our site, even just yesterday I found this kind of problem. My actual site is in its own server, I wanted to upload files from another server(admin panel). So for that I found this way very useful. And now I am sharing my code with you.

On a button click, get the FileUploader to get the file name and the user-name, password of the ftp server.

Now write the following code into the Button_Click event.

Upload("ftp://ftpserver.com""user-name""password", "<Your file name from FileUploader>");

And in the Upload method write down the code.

private static void Upload(string ftpServer, string userName, string password, string filename)
{
   using (System.Net.WebClient client = new System.Net.WebClient())
   {
      client.Credentials = new System.Net.NetworkCredential(userName, password);
      client.UploadFile(ftpServer + "/" + new FileInfo(filename).Name, "STOR", filename);
   }
}


If your website does not have a ftp server then open the parallel plux and create a ftp server with user-name and password, and then try to upload the files.

For more ftp upload info you can visit the MSDN website of Microsoft.


Ref : http://madskristensen.net/post/simple-ftp-file-upload-in-c-20

1 comments:

Post a Comment

Follow by Email

Popular Posts

Pageviews