Reputation: 175
I am trying to upload an image to FTP server. But I'm getting an error
The remote server returned an error: (550) File unavailable (e.g., file not found, no access).
Here is my code:
public void Upload(string fileName, string base64, string path)
{
var bytes = Convert.FromBase64String(base64);
var uri = new Uri($"ftp://{Host}/{path}/{fileName}");
var request = (FtpWebRequest)WebRequest.Create(uri);
request.Method = WebRequestMethods.Ftp.UploadFile;
request.UsePassive = false;
request.Credentials = new NetworkCredential(Username, Password);
request.ContentLength = bytes.Length;
request.UseBinary = true;
request.KeepAlive = false;
using (var requestStream = request.GetRequestStream())
{
requestStream.Write(bytes, 0, bytes.Length);
requestStream.Close();
}
using (var response = (FtpWebResponse)request.GetResponse())
{
if (response != null)
response.Close();
}
}
My Host
is something like this: localhost:port-number
.
The path is a folder named Images
.
So I want to save the image at localhost:port-number/Images
but I am getting that error.
When I open the FTP point from the browser, it works fine and I can see the content. What is wrong here?
Upvotes: 1
Views: 2308
Reputation: 202088
In general, setting FtpWebRequest.UsePassive
to false
is a bad idea. Stick with the default true
, unless you have a good reason to use the active mode.
Read my article on the FTP connection modes to understand why.
The server probably returns a relevant error message with the 550
code. But the FTP implementation in .NET framework translates all FTP status codes to its own (localized) message. Particularly code 550 is translated to "File unavailable". That, in some cases (like probably this one), hides away the real problem.
Upvotes: 1