I am trying to make a program that uploads/downloads .exe
file to a FTP
I tried using FtpWebRequest
, but I only succeed to upload and download .txt files.
Then i found here a solution for downloading using the WebClient
:
WebClient request = new WebClient();
request.Credentials = new NetworkCredential("username", "password");
byte[] fileData = request.DownloadData("ftp://myFTP.net/");
FileStream file = File.Create(destinatie);
file.Write(fileData, 0, fileData.Length);
file.Close();
This solution works. But I seen that WebClient
has a method DownloadFile
which did not worked. I think because it doesn't work on FTP
only on HTTP
. Is my assumption true? If not how can I get it to work?
And is there any other solution for uploading/downloading a .exe
file to ftp using FtpWebRequest
?
Uploading via FTP clientIf you have an FTP client like FileZilla, transferring files is a simple three-step process. Open FileZilla from your desktop or Start menu. Type in the following at the top and click Quickconnect. Drag and drop the relevant files into the upload folder.
FTP transfers require an Internet connection. Downloading files means transferring a file from a server to a computer or device, while uploading is the opposite—transferring a file from a computer to a server.
We use FTP for downloading as well as uploading files between a server and a client over the internet. We use HTTP for providing various web pages from the web browser to the web server. FTP makes use of a persistent connection of TCP for its control connection and a non-persistent one for its data connection.
Both WebClient.UploadFile
and WebClient.DownloadFile
work correctly for FTP as well as binary files.
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, transfer resuming, etc), use FtpWebRequest
. Easy way is to just copy a FileStream
to 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:
How can we show progress bar for upload with FtpWebRequest
If you want to upload all files from a folder, see
Recursive upload to FTP server in C#.
WebClient client = new WebClient();
client.Credentials = new NetworkCredential("username", "password");
client.DownloadFile(
"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, resuming transfers, etc), use FtpWebRequest
. Easy way is to just copy an FTP response stream to FileStream
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.DownloadFile;
using (Stream ftpStream = request.GetResponse().GetResponseStream())
using (Stream fileStream = File.Create(@"C:\local\path\file.zip"))
{
ftpStream.CopyTo(fileStream);
}
If you need to monitor a download 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.DownloadFile;
using (Stream ftpStream = request.GetResponse().GetResponseStream())
using (Stream fileStream = File.Create(@"C:\local\path\file.zip"))
{
byte[] buffer = new byte[10240];
int read;
while ((read = ftpStream.Read(buffer, 0, buffer.Length)) > 0)
{
fileStream.Write(buffer, 0, read);
Console.WriteLine("Downloaded {0} bytes", fileStream.Position);
}
}
For GUI progress (WinForms ProgressBar
), see:
FtpWebRequest FTP download with ProgressBar
If you want to download all files from a remote folder, see
C# Download all files and subdirectories through FTP.
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