FtpWebRequest error: 550 Size not allowed in ASCII mode

I am trying to get the file size from a remote FTP file via 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; } } 

The error "550 Size is not allowed in ASCII mode" is currently being returned. I know that I need to use binary mode, but setting UseBinary to true (see above) does not fix the problem.

+2
c # binary ascii ftp ftpwebrequest
Dec 22 '14 at 0:30
source share
1 answer

Sorry, I think you can get stuck. The WebRequestMethods.Ftp , for this post , will not support sending FTP commands other than supported, and for your use case, you will need your client to send "TYPE I" (for "image" or binary mode) before sending the SIZE command.

In addition, as a hacker workaround, you can try downloading the file β€” any file β€” before sending the SIZE command. With request.UseBinary = true for this request, it should force your client to send the "TYPE I" command to the FTP server. (And it doesn’t matter if the download request fails, the TYPE command will still be sent.) Most FTP servers that receive the TYPE command will consider TYPE for subsequent commands. Then, when you try the GetFileSize request GetFileSize , the FTP server may be in binary rather than ASCII mode, and your SIZE command may succeed.

+1
Jan 18 '16 at 2:51 on
source share



All Articles