Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

FtpWebRequest Download File

The following code is intended to retrieve a file via FTP. However, I'm getting an error with it.

serverPath = "ftp://x.x.x.x/tmp/myfile.txt";  FtpWebRequest request = (FtpWebRequest)WebRequest.Create(serverPath);  request.KeepAlive = true; request.UsePassive = true; request.UseBinary = true;  request.Method = WebRequestMethods.Ftp.DownloadFile;                 request.Credentials = new NetworkCredential(username, password);  // Read the file from the server & write to destination                 using (FtpWebResponse response = (FtpWebResponse)request.GetResponse()) // Error here using (Stream responseStream = response.GetResponseStream()) using (StreamReader reader = new StreamReader(responseStream))             using (StreamWriter destination = new StreamWriter(destinationFile)) {     destination.Write(reader.ReadToEnd());     destination.Flush(); } 

The error is:

The remote server returned an error: (550) File unavailable (e.g., file not found, no access)

The file definitely does exist on the remote machine and I am able to perform this ftp manually (i.e. I have permissions). Can anyone tell me why I might be getting this error?

like image 560
Paul Michaels Avatar asked May 06 '10 14:05

Paul Michaels


People also ask

How do I download an FTP file?

To download a file, drag the file from the browser window to the desktop. You can also double-click the filename, and you will be prompted to either save or open the file. To upload a file, drag the file from your hard drive to the browser window.


2 Answers

I know this is an old Post but I am adding here for future reference. Here is a solution that I found:

    private void DownloadFileFTP()     {         string inputfilepath = @"C:\Temp\FileName.exe";         string ftphost = "xxx.xx.x.xxx";         string ftpfilepath = "/Updater/Dir1/FileName.exe";          string ftpfullpath = "ftp://" + ftphost + ftpfilepath;          using (WebClient request = new WebClient())         {             request.Credentials = new NetworkCredential("UserName", "P@55w0rd");             byte[] fileData = request.DownloadData(ftpfullpath);              using (FileStream file = File.Create(inputfilepath))             {                 file.Write(fileData, 0, fileData.Length);                 file.Close();             }             MessageBox.Show("Download Complete");         }     } 

Updated based upon excellent suggestion by Ilya Kogan

like image 96
Mark Kram Avatar answered Sep 30 '22 20:09

Mark Kram


Easiest way

The most trivial way to download a binary file from an FTP server using .NET framework is using WebClient.DownloadFile:

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"); 

Advanced options

Use FtpWebRequest, only if you need a greater control, that WebClient does not offer (like TLS/SSL encryption, progress monitoring, ascii/text transfer mode, resuming transfers, etc). 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); } 

Progress monitoring

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


Downloading folder

If you want to download all files from a remote folder, see
C# Download all files and subdirectories through FTP.

like image 40
Martin Prikryl Avatar answered Sep 30 '22 19:09

Martin Prikryl