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 !"); } }
File Transfer Protocol (FTP) is a network protocol for file transfer.
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.
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); }
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");
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); }
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
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#
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With