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 !");
}
}
The existing answers are valid, but why re-invent the wheel and bother with lower level
WebRequest
types whileWebClient
already implements FTP uploading neatly:Easiest way
The most trivial way to upload a file to an FTP server using .NET framework is using
WebClient.UploadFile
method:Advanced options
If you need a greater control, that
WebClient
does not offer (like TLS/SSL encryption, ASCII mode, active mode, etc), useFtpWebRequest
. Easy way is to just copy aFileStream
to an FTP stream usingStream.CopyTo
:Progress monitoring
If you need to monitor an upload progress, you have to copy the contents by chunks yourself:
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 using WebClient.
How to use
use this in your foreach
and you only need to create folder one time
to create a folder
The following works for me:
You can't read send the file parameter in your code as it is only the filename.
Use the following:
To get the file so you can pass it to the
Send
method.publish date: 06/26/2018
https://docs.microsoft.com/en-us/dotnet/framework/network-programming/how-to-upload-files-with-ftp
In the first example must change those to:
First flush and after that close.