locked
Unable to upload file using FTP. RRS feed

  • Question

  • User-1614001388 posted

    Hi,

    I'm unable to upload a text file to remote server using ftp. I'm getting the error message "227 Entering passive mode ", while getting the request stream.  Please help me in finding what's wrong with that(problem with my code or server).

     I'm using the following code to do that:

    // create a ftp object with the address to communicate

    Uri target = new Uri(address);

    FtpWebRequest ftpRequest = (FtpWebRequest)WebRequest.Create(target);

    // specify the type of ftp request to perform

    ftpRequest.Method = WebRequestMethods.Ftp.UploadFile;

    ftpRequest.UseBinary = true;

    // specify the username and password

    ftpRequest.Credentials = new NetworkCredential(username, password);

    //copy the contents of the file into a byte array

    StreamReader sourceStream = new StreamReader(filepath);

    Byte[] btFileData = Encoding.UTF8.GetBytes(sourceStream.ReadToEnd());

    sourceStream.Close();

    ftpRequest.ContentLength = btFileData.Length;

    try

    {

    // write the array of data into a requet stream

    Stream requestStream = ftpRequest.GetRequestStream();

    requestStream.Write(btFileData, 0, btFileData.Length);

    requestStream.Close();

    }

    catch(Exception e)

    {

    MessageBox.Show(e.Message,"Could not get the request stream");

    return;

    }

    try

    {

    FtpWebResponse ftpResponse = (FtpWebResponse)ftpRequest.GetResponse();

    }

    catch (Exception e)

    {

    MessageBox.Show(e.Message,"Cannot get the response");

    return;

    }

     

     

    Friday, March 24, 2006 12:01 PM

All replies

  • User-1853252149 posted

    That response code isn't an error, just a notification.  Server and client side must both use the same mode, either passive or active.  Not sure completely why it's showing for you.

    Jeff

    Friday, March 24, 2006 1:46 PM
  • User989702501 posted
    Was it failed to upload? have you try using ftp client to connect via passive mode first before testing the code?
    Saturday, March 25, 2006 3:13 AM
  • User-376941307 posted

    Hi,

    I was having exactly the same problem and the answer (in my case at least) was to prevent the connection using passive mode.

    Before you do your call to ftpRequest.GetRequestStream() try:

    ftpRequest.UsePassive = false;

    Hope this helps!

    Pete

    Tuesday, March 28, 2006 12:41 PM
  • User989702501 posted

    Or you can make sure that passive mode traffic is allowed.... make sure firewall/router/etc are not blocking the outbound data.  By default passive port range from 1024-5000, if you on IIS 6, you can customize it.
    How To Configure PassivePortRange In IIS
    http://support.microsoft.com/?id=555022

    Tuesday, March 28, 2006 9:38 PM
  • User-376941307 posted

    True, except that my transfer needs were within a tightly locked down system and (this will often be the case) I had no control whatsoever over any firewall/router settings.

    Pete

    Wednesday, March 29, 2006 3:00 AM
  • User989702501 posted
    [:D]
    Wednesday, March 29, 2006 9:29 PM
  • User-1750108852 posted

    That worked for me!

    Thanks,
    Staik

    PS: 2 too often forgotten lines:
    ftp.Proxy = null;
    ftp.UsePassive = false;

    Wednesday, June 6, 2007 10:02 AM