FtpWebRequest error: 550 Size not allowed in ASCII mode
Asked Answered
A

1

3

I'm trying to get the file size from a remote FTP file through anonymous FTP.

public static long GetSize(string ftpPath)
{
    try
    {
        FtpWebRequest request = (FtpWebRequest)FtpWebRequest.Create(new Uri(ftpPath));
        request.Proxy = null;
        request.Credentials = new NetworkCredential("anonymous", "´");
        request.UseBinary = true;
        request.Method = WebRequestMethods.Ftp.GetFileSize;

        FtpWebResponse response = (FtpWebResponse)request.GetResponse();
        long size = response.ContentLength;
        response.Close();
        return size;
    }
    catch (WebException e)
    {
        string status = ((FtpWebResponse)e.Response).StatusDescription;
        MessageBox.Show(status);
        return 0;
    }
}

This currently returns the error "550 Size not allowed in ASCII mode." I'm aware that I have to use binary mode, but setting UseBinary to true (see above) doesn't fix the issue.

Astral answered 22/12, 2014 at 0:30 Comment(0)
I
2

Unfortunately, I think you may be stuck. The WebRequestMethods.Ftp class, per this post, will not support sending FTP commands other than the supported ones -- and for your use case, you would need your client to send "TYPE I" (for "image" or binary mode) before sending the SIZE command.

Alternatively, as a hacky workaround, you might try download a file -- any file -- before sending your SIZE command. With request.UseBinary = true for that request, it should cause your client to send the "TYPE I" command to the FTP server. (And it won't matter if that download request fails; the TYPE command will still have been sent.) Most FTP servers, upon receiving a TYPE command, will assume that TYPE for subsequent commands. Then, when you try the GetFileSize request again, the FTP server might be in binary, not ASCII mode, and your SIZE command might succeed.

Interdictory answered 18/1, 2016 at 2:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.