2
votes

i'm trying to upload file to ftp server. tried some code samples, but alway getting this error, entering passive mode. for example, i can create a directory with this code

FtpWebRequest reqFTP;
try
{
    // dirName = name of the directory to create.
    reqFTP = (FtpWebRequest)FtpWebRequest.Create(
             new Uri("ftp://" + ftpServerIP + "/" + dirName));
    reqFTP.Method = WebRequestMethods.Ftp.MakeDirectory;
    reqFTP.UseBinary = true;
    reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
    reqFTP.UsePassive = false;
    FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
    Stream ftpStream = response.GetResponseStream();

    ftpStream.Close();
    response.Close();
}
catch (Exception ex)
{
    MessageBox.Show(ex.Message);
}

or for example i can rename a file. but cannot upload file with this code

string uri = "ftp://" + ftpServerIP + "/" + fileInf.Name;
FtpWebRequest reqFTP;
reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(
         "ftp://" + ftpServerIP + "/" + fileInf.Name));
reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
reqFTP.KeepAlive = false;
reqFTP.Method = WebRequestMethods.Ftp.UploadFile;
reqFTP.UseBinary = true;
reqFTP.ContentLength = fileInf.Length;

int buffLength = 2048;
byte[] buff = new byte[buffLength];
int contentLen;

FileStream fs = fileInf.OpenRead();

try
{
    Stream strm = reqFTP.GetRequestStream();
    contentLen = fs.Read(buff, 0, buffLength);
    while (contentLen != 0)
    {
        strm.Write(buff, 0, contentLen);
        contentLen = fs.Read(buff, 0, buffLength);
    }
    strm.Close();
    fs.Close();
}
catch(Exception ex)
{
    MessageBox.Show(ex.Message, "Upload Error");
}

getting exception at reqFTP.GetRequestStream().

If I use reqFTP.UsePassive=false then i get “

The remote server returned an error: (500) Syntax error, command unrecognized”.

What should i do?

2
Anyone having this problem, append a log to the question (or post a new question and link it here). Without a log, the question is not answerable.Martin Prikryl
I had this problem today due to Kaspersky antivirus. If I suspend kaspersky and retry everything works as expectedxrodas

2 Answers

1
votes

Try this example

http://social.msdn.microsoft.com/Forums/en-US/0128e595-c8e2-4f5e-9426-fd93eb510cab/the-remote-server-returned-an-error-227-entering-passive-mode-67228534212130

If you set UsePassive to false, then you need to make sure that the port for the command channel is open (i.e., you need to define endpoints and access rules). Unless there is a good reason to not use passive, you are far better off using passive.

Hope it will help.

0
votes

So, I know this is a late answer but I wanted to share my experience in case someone will have the same thing.

In my case, I was downloading some files from a Windows Server 2016. For some security reasons, I activated the firewall, and ofcourse I added an inbound rule to the firewall to allow 20 and 21 ports.

I had the same famous error: 227 entering passive mode. I checked the code, in the error, it was allways some different ports indicatd.

After some search I found that I had to add 49152-65534 among allowed ports.

And it worked.

Here is my code

   public static bool DownloadDocument(string ftpPath, string downloadPath)
    {
        bool retVal = false;
        try
        {
            if (!Directory.Exists(Tools.LocalPath))
                Directory.CreateDirectory(Tools.LocalPath);

            using (WebClient client = new WebClient())
            {
                client.Credentials = new NetworkCredential(Tools.FtpUserName, Tools.FtpPassword);
             
                client.DownloadFile(ftpPath, downloadPath);
            }
            retVal = true;
        }
        catch (Exception ex)
        {
            UserMethods.ParseError(ex, "DownloadFile");
        }

        return retVal;
    }