Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Upload file to FTP using C#

I try upload a file to an FTP-server with C#. The file is uploaded but with zero bytes.

private void button2_Click(object sender, EventArgs e) {     var dirPath = @"C:/Documents and Settings/sander.GD/Bureaublad/test/";      ftp ftpClient = new ftp("ftp://example.com/", "username", "password");      string[] files = Directory.GetFiles(dirPath,"*.*");      var uploadPath = "/httpdocs/album";      foreach (string file in files)     {         ftpClient.createDirectory("/test");          ftpClient.upload(uploadPath + "/" + Path.GetFileName(file), file);     }      if (string.IsNullOrEmpty(txtnaam.Text))     {         MessageBox.Show("Gelieve uw naam in te geven !");     } } 
like image 253
webvision Avatar asked Mar 07 '13 10:03

webvision


People also ask

What is FTP in C#?

File Transfer Protocol (FTP) is a network protocol for file transfer.

How do I upload to FTP using terminal?

Upload Single File to FTP Server To upload file on FTP server use put command from FTP prompt. First, navigate to the desired directory on the FTP server where to upload a file and use the following command. It will upload local system file c:\files\file1. txt to uploads directory on FTP server.


2 Answers

The existing answers are valid, but why re-invent the wheel and bother with lower level WebRequest types while WebClient already implements FTP uploading neatly:

using (var client = new WebClient()) {     client.Credentials = new NetworkCredential(ftpUsername, ftpPassword);     client.UploadFile("ftp://host/path.zip", WebRequestMethods.Ftp.UploadFile, localFile); } 
like image 63
Saeb Amini Avatar answered Sep 20 '22 16:09

Saeb Amini


Easiest way

The most trivial way to upload a file to an FTP server using .NET framework is using WebClient.UploadFile method:

WebClient client = new WebClient(); client.Credentials = new NetworkCredential("username", "password"); client.UploadFile(     "ftp://ftp.example.com/remote/path/file.zip", @"C:\local\path\file.zip"); 

Advanced options

If you need a greater control, that WebClient does not offer (like TLS/SSL encryption, ascii/text transfer mode, active mode, transfer resuming, progress monitoring, etc), use FtpWebRequest. Easy way is to just copy a FileStream to an FTP stream using Stream.CopyTo:

FtpWebRequest request =     (FtpWebRequest)WebRequest.Create("ftp://ftp.example.com/remote/path/file.zip"); request.Credentials = new NetworkCredential("username", "password"); request.Method = WebRequestMethods.Ftp.UploadFile;    using (Stream fileStream = File.OpenRead(@"C:\local\path\file.zip")) using (Stream ftpStream = request.GetRequestStream()) {     fileStream.CopyTo(ftpStream); } 

Progress monitoring

If you need to monitor an upload progress, you have to copy the contents by chunks yourself:

FtpWebRequest request =     (FtpWebRequest)WebRequest.Create("ftp://ftp.example.com/remote/path/file.zip"); request.Credentials = new NetworkCredential("username", "password"); request.Method = WebRequestMethods.Ftp.UploadFile;    using (Stream fileStream = File.OpenRead(@"C:\local\path\file.zip")) using (Stream ftpStream = request.GetRequestStream()) {     byte[] buffer = new byte[10240];     int read;     while ((read = fileStream.Read(buffer, 0, buffer.Length)) > 0)     {         ftpStream.Write(buffer, 0, read);         Console.WriteLine("Uploaded {0} bytes", fileStream.Position);     }  } 

For GUI progress (WinForms ProgressBar), see C# example at:
How can we show progress bar for upload with FtpWebRequest


Uploading folder

If you want to upload all files from a folder, see
Upload directory of files to FTP server using WebClient.

For a recursive upload, see
Recursive upload to FTP server in C#

like image 21
Martin Prikryl Avatar answered Sep 20 '22 16:09

Martin Prikryl