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?
This paragraph from the FptWebRequest class reference might be of interest to you:
I know this is an old Post but I am adding here for future reference. Here is a solution that I found:
Updated based upon excellent suggestion by Ilya Kogan
The most trivial way to download a binary file from an FTP server using .NET framework is using
WebClient.DownloadFile
:Use
FtpWebRequest
, only if you need a greater control, thatWebClient
does not offer (like TLS/SSL encryption, progress monitoring etc). Easy way is to just copy an FTP response stream toFileStream
usingStream.CopyTo
:If you need to monitor a download progress, you have to copy the contents by chunks yourself:
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.
I hope it can help you.
I had the same issue!
My solution was to insert the
public_html
folder into the download URL.Real file location on the server:
Web URL: