简体   繁体   中英

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

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 (eg, 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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM