Ask a questionAsk a question
 

AnswerFtp Upload File

  • Thursday, August 10, 2006 7:39 AMkarande23 Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    This is my code snippet for file upload. The problem is after all the bytes are written on the server, while doing requestStream.Close() I get error as underlying connection was closed.

                   FtpWebRequest request = (FtpWebRequest)WebRequest.Create(serverUri);
                    request.UsePassive = false;
                    request.Method = WebRequestMethods.Ftp.UploadFile;        
                    request.Credentials = new NetworkCredential("xyz", "xyz");                  

                    Stream requestStream = request.GetRequestStream();

                    const int bufferLength = 2048;
                    byte[] buffer = new byte[bufferLength];
                    int count = 0;
                    int readBytes = 0;
                    FileStream stream = File.OpenRead(fileName);

                    do
                    {
                        readBytes = stream.Read(buffer, 0, bufferLength);
                        requestStream.Write(buffer, 0, readBytes);
                        count += readBytes;                
                    }
                    while (readBytes != 0);

                    requestStream.Flush();              
                    requestStream.Close();//get an error here. Also dont think I can avoid this                                                                //statement.

                    FtpWebResponse response = (FtpWebResponse)request.GetResponse();                              response.Close();

    I am using VS 2005 and the ftp server is Microsoft Ftp server.

Answers

  • Monday, August 21, 2006 4:36 PMMariya Atanasova [NCL]MSFT, ModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer

    This code worked for me and I didn't get any exceptions. I tried with a simple txt file in the current directory. Note: since you're using active ftp you have to set the proxy to null.  

    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Net;
    using System.IO;

    namespace FtpWebRequest_MSDN2
    {
    class Program
    {
    static void Main(string[] args)
    {
    string serverUri = ftp://yourIP/textFile.txt;
    FtpWebRequest request = (FtpWebRequest)WebRequest.Create(serverUri);
    request.UsePassive =
    false;
    request.Method =
    WebRequestMethods.Ftp.UploadFile;
    request.Credentials =
    new NetworkCredential("user", "pass");
    request.Proxy =
    null;
    Stream requestStream = request.GetRequestStream();
    const int bufferLength = 2048;
    byte[] buffer = new byte[bufferLength];
    int count = 0;
    int readBytes = 0;
    FileStream stream = File.OpenRead("textFile.txt");
    do
    {
    readBytes = stream.Read(buffer, 0, bufferLength);
    requestStream.Write(buffer, 0, readBytes);
    count += readBytes;
    }
    while (readBytes != 0);
    requestStream.Close();
    FtpWebResponse response = (FtpWebResponse)request.GetResponse();
    Stream respStream = response.GetResponseStream();
    respStream.Close();
    response.Close();
    }
    }

    This is just a sample without try-catch and any other checks to get you started. I've used as much of your code as I could to make it easier for you to find the problem. 90% of the time the problem with  active ftp connections is that your firewall or antivirus software block the response from the server and when we don't receive it we consider the connection didn't succeed and throw an exception.

    Mariya

All Replies

  • Thursday, August 10, 2006 3:07 PMMariya Atanasova [NCL]MSFT, ModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    Does serverUri contain your filename as well? it has to be something like that serverUri=ftp://servername/path/fileName ?

    Also, try commenting the line containing the Flush

    Mariya

  • Saturday, August 12, 2006 5:46 AMkarande23 Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    the serverUri is right coz the file gets uploaded, I can stream that file and see...

    Also commenting flush does not help.
    Can this be a problem of server...I am using Microsoft Ftp Server.
  • Monday, August 14, 2006 4:28 PMMariya Atanasova [NCL]MSFT, ModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    The reasons to receive "The Underlying Connection closed" exception can be quite different. Bad server configuration is just one of the possible reasons. Can you get a System.Net trace log and a netmon trace anf post them here?

    Here are instructions on how to get a trace log and a netmon trace

    http://blogs.msdn.com/dgorti/archive/2005/09/18/471003.aspx
    http://blogs.msdn.com/dgorti/archive/2005/10/29/486887.aspx

    Mariya

  • Tuesday, August 15, 2006 2:12 AMMariya Atanasova [NCL]MSFT, ModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    I tested your code on our FtpServers (by FtpServer I mean FtpService running under IIS) and it worked fine.

    Since you're setting UsePassive to false it is very probable taht your firewall is blocking the connection coming from the ftp server. Try stopping the firewall and exceuting it again to see if it will work.

     

    Mariya

  • Wednesday, August 16, 2006 5:27 AMkarande23 Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    yeh, I will try that...sorry for late reply was on holiday for two days.
    I will try the above two ways, lets see wat works...
  • Wednesday, August 16, 2006 6:21 AMkarande23 Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    this is the log that I get....

    System.Net Verbose: 0 : [3260] WebRequest::Create(ftp://71.6.135.39/uservideos/Shri.wmv)
    System.Net Information: 0 : [3260] FtpWebRequest#28068188::.ctor(ftp://71.6.135.39/uservideos/Shri.wmv)
    System.Net Verbose: 0 : [3260] Exiting WebRequest::Create() -> FtpWebRequest#28068188
    System.Net Verbose: 0 : [3260] FtpWebRequest#28068188::GetRequestStream()
    System.Net Information: 0 : [3260] FtpWebRequest#28068188::GetRequestStream(Method=STOR.)
    System.Net Information: 0 : [3260] Associating FtpWebRequest#28068188 with FtpControlStream#33163964
    System.Net Information: 0 : [3260] FtpControlStream#33163964 - Received response [220 Microsoft FTP Service]
    System.Net Information: 0 : [3260] FtpControlStream#33163964 - Sending command [USER piot]
    System.Net Information: 0 : [3260] FtpControlStream#33163964 - Received response [331 Password required for piot.]
    System.Net Information: 0 : [3260] FtpControlStream#33163964 - Sending command [PASS ********]
    System.Net Information: 0 : [3260] FtpControlStream#33163964 - Received response [230-Piot FTP
    230 User piot logged in.]
    System.Net Information: 0 : [3260] FtpControlStream#33163964 - Sending command [OPTS utf8 on]
    System.Net Information: 0 : [3260] FtpControlStream#33163964 - Received response [501 option not supported]
    System.Net Information: 0 : [3260] FtpControlStream#33163964 - Sending command [PWD]
    System.Net Information: 0 : [3260] FtpControlStream#33163964 - Received response [257 "/" is current directory.]
    System.Net Information: 0 : [3260] FtpControlStream#33163964 - Sending command [CWD /uservideos/]
    System.Net Information: 0 : [3260] FtpControlStream#33163964 - Received response [250 CWD command successful.]
    System.Net Information: 0 : [3260] FtpControlStream#33163964 - Sending command [TYPE I]
    System.Net Information: 0 : [3260] FtpControlStream#33163964 - Received response [200 Type set to I.]
    System.Net Information: 0 : [3260] FtpControlStream#33163964 - Sending command [PORT 192,168,2,2,5,192]
    System.Net Information: 0 : [3260] FtpControlStream#33163964 - Received response [200 PORT command successful.]
    System.Net Information: 0 : [3260] FtpControlStream#33163964 - Sending command [STOR Shri.wmv]
    System.Net Information: 0 : [3260] FtpControlStream#33163964 - Received response [150 Opening BINARY mode data connection for Shri.wmv.]
    System.Net Verbose: 0 : [3260] Exiting FtpWebRequest#28068188::GetRequestStream()
    System.Net Information: 0 : [3260] FtpWebRequest#28068188::(Releasing FTP connection#33163964.)

  • Wednesday, August 16, 2006 4:24 PMMariya Atanasova [NCL]MSFT, ModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    Is this the whole trace? According to what you have pasted here the connection is established just fine and  I don't see any exceptions. Did you try stopping the firewall?

    Mariya

  • Thursday, August 17, 2006 6:36 AMkarande23 Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    The last line in the Trace says Releasing....it is not released?
    There is no mention 220 command which states Transfer complete.
  • Thursday, August 17, 2006 6:41 AMkarande23 Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    This is trace while transfering a small file...basically the small and big files get transfered properly if the server is local, while on remote server it gives a problem.

    System.Net Verbose: 0 : [3060] WebRequest::Create(ftp://71.6.135.39/uservideos/Sara.wmv)
    System.Net Information: 0 : [3060] FtpWebRequest#28068188::.ctor(ftp://71.6.135.39/uservideos/Sara.wmv)
    System.Net Verbose: 0 : [3060] Exiting WebRequest::Create()     -> FtpWebRequest#28068188
    System.Net Verbose: 0 : [3060] FtpWebRequest#28068188::GetRequestStream()
    System.Net Information: 0 : [3060] FtpWebRequest#28068188::GetRequestStream(Method=STOR.)
    System.Net Information: 0 : [3060] Associating FtpWebRequest#28068188 with FtpControlStream#33163964
    System.Net Information: 0 : [3060] FtpControlStream#33163964 - Received response [220 AksTech FTP server ready...]
    System.Net Information: 0 : [3060] FtpControlStream#33163964 - Sending command [USER piot]
    System.Net Information: 0 : [3060] FtpControlStream#33163964 - Received response [331 User piot okay, need password.]
    System.Net Information: 0 : [3060] FtpControlStream#33163964 - Sending command [PASS ********]
    System.Net Information: 0 : [3060] FtpControlStream#33163964 - Received response [230-command ok
    230 User logged in, proceed.]
    System.Net Information: 0 : [3060] FtpControlStream#33163964 - Sending command [OPTS utf8 on]
    System.Net Information: 0 : [3060] FtpControlStream#33163964 - Received response [200 UTF8 OPTS ON]
    System.Net Information: 0 : [3060] FtpControlStream#33163964 - Sending command [PWD]
    System.Net Information: 0 : [3060] FtpControlStream#33163964 - Received response [257 "/" is current directory.]
    System.Net Information: 0 : [3060] FtpControlStream#33163964 - Sending command [CWD /uservideos/]
    System.Net Information: 0 : [3060] FtpControlStream#33163964 - Received response [250 /uservideos now current directory.]
    System.Net Information: 0 : [3060] FtpControlStream#33163964 - Sending command [TYPE I]
    System.Net Information: 0 : [3060] FtpControlStream#33163964 - Received response [200 Command okay]
    System.Net Information: 0 : [3060] FtpControlStream#33163964 - Sending command [PORT 192,168,2,2,7,137]
    System.Net Information: 0 : [3060] FtpControlStream#33163964 - Received response [200 Command okay]
    System.Net Information: 0 : [3060] FtpControlStream#33163964 - Sending command [STOR Sara.wmv]
    System.Net Information: 0 : [3060] FtpControlStream#33163964 - Received response [150 Status OK, about to open data connection.]
    System.Net Verbose: 0 : [3060] Exiting FtpWebRequest#28068188::GetRequestStream()
    System.Net Verbose: 0 : [3764] WebRequest::Create(ftp://71.6.135.39/uservideos/Sara.wmv)
    System.Net Information: 0 : [3764] FtpWebRequest#28068188::.ctor(ftp://71.6.135.39/uservideos/Sara.wmv)
    System.Net Verbose: 0 : [3764] Exiting WebRequest::Create()     -> FtpWebRequest#28068188
    System.Net Verbose: 0 : [3764] FtpWebRequest#28068188::GetRequestStream()
    System.Net Information: 0 : [3764] FtpWebRequest#28068188::GetRequestStream(Method=STOR.)
    System.Net Information: 0 : [3764] Associating FtpWebRequest#28068188 with FtpControlStream#33163964
    System.Net Information: 0 : [3764] FtpControlStream#33163964 - Received response [220 AksTech FTP server ready...]
    System.Net Information: 0 : [3764] FtpControlStream#33163964 - Sending command [USER piot]
    System.Net Information: 0 : [3764] FtpControlStream#33163964 - Received response [331 User piot okay, need password.]
    System.Net Information: 0 : [3764] FtpControlStream#33163964 - Sending command [PASS ********]
    System.Net Information: 0 : [3764] FtpControlStream#33163964 - Received response [230-command ok
    230 User logged in, proceed.]
    System.Net Information: 0 : [3764] FtpControlStream#33163964 - Sending command [OPTS utf8 on]
    System.Net Information: 0 : [3764] FtpControlStream#33163964 - Received response [200 UTF8 OPTS ON]
    System.Net Information: 0 : [3764] FtpControlStream#33163964 - Sending command [PWD]
    System.Net Information: 0 : [3764] FtpControlStream#33163964 - Received response [257 "/" is current directory.]
    System.Net Information: 0 : [3764] FtpControlStream#33163964 - Sending command [CWD /uservideos/]
    System.Net Information: 0 : [3764] FtpControlStream#33163964 - Received response [250 /uservideos now current directory.]
    System.Net Information: 0 : [3764] FtpControlStream#33163964 - Sending command [TYPE I]
    System.Net Information: 0 : [3764] FtpControlStream#33163964 - Received response [200 Command okay]
    System.Net Information: 0 : [3764] FtpControlStream#33163964 - Sending command [PORT 192,168,2,2,7,139]
    System.Net Information: 0 : [3764] FtpControlStream#33163964 - Received response [200 Command okay]
    System.Net Information: 0 : [3764] FtpControlStream#33163964 - Sending command [STOR Sara.wmv]
    System.Net Information: 0 : [3764] FtpControlStream#33163964 - Received response [150 Status OK, about to open data connection.]
    System.Net Verbose: 0 : [3764] Exiting FtpWebRequest#28068188::GetRequestStream()
    System.Net Information: 0 : [3764] FtpControlStream#33163964 - Received response [226- command ok
    226-Closing data connection.
    226 - [Section: ALL] - [Free: 11.52 G] - [Dl: 1.02 M] - [Ul: 69.72 M] -]
    System.Net Information: 0 : [3764] FtpWebRequest#28068188::(Releasing FTP connection#33163964.)
    System.Net Verbose: 0 : [3764] FtpWebRequest#28068188::GetResponse()
    System.Net Information: 0 : [3764] FtpWebRequest#28068188::GetResponse(Method=STOR.)
    System.Net Verbose: 0 : [3764] Exiting FtpWebRequest#28068188::GetResponse()
    System.Net Verbose: 0 : [3764] FtpWebResponse#14421545::Close()
    System.Net Verbose: 0 : [3764] Exiting FtpWebResponse#14421545::Close()

  • Thursday, August 17, 2006 7:14 AMkarande23 Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    The above one is succesfull transfer trace, while the below one is not:

    System.Net Verbose: 0 : [3404] WebRequest::Create(ftp://71.6.135.39/uservideos/Shri1.wmv)
    System.Net Information: 0 : [3404] FtpWebRequest#28068188::.ctor(ftp://71.6.135.39/uservideos/Shri1.wmv)
    System.Net Verbose: 0 : [3404] Exiting WebRequest::Create()     -> FtpWebRequest#28068188
    System.Net Verbose: 0 : [3404] FtpWebRequest#28068188::GetRequestStream()
    System.Net Information: 0 : [3404] FtpWebRequest#28068188::GetRequestStream(Method=STOR.)
    System.Net Information: 0 : [3404] Associating FtpWebRequest#28068188 with FtpControlStream#33163964
    System.Net Information: 0 : [3404] FtpControlStream#33163964 - Received response [220 AksTech FTP server ready...]
    System.Net Information: 0 : [3404] FtpControlStream#33163964 - Sending command [USER piot]
    System.Net Information: 0 : [3404] FtpControlStream#33163964 - Received response [331 User piot okay, need password.]
    System.Net Information: 0 : [3404] FtpControlStream#33163964 - Sending command [PASS ********]
    System.Net Information: 0 : [3404] FtpControlStream#33163964 - Received response [230-command ok
    230 User logged in, proceed.]
    System.Net Information: 0 : [3404] FtpControlStream#33163964 - Sending command [OPTS utf8 on]
    System.Net Information: 0 : [3404] FtpControlStream#33163964 - Received response [200 UTF8 OPTS ON]
    System.Net Information: 0 : [3404] FtpControlStream#33163964 - Sending command [PWD]
    System.Net Information: 0 : [3404] FtpControlStream#33163964 - Received response [257 "/" is current directory.]
    System.Net Information: 0 : [3404] FtpControlStream#33163964 - Sending command [CWD /uservideos/]
    System.Net Information: 0 : [3404] FtpControlStream#33163964 - Received response [250 /uservideos now current directory.]
    System.Net Information: 0 : [3404] FtpControlStream#33163964 - Sending command [TYPE I]
    System.Net Information: 0 : [3404] FtpControlStream#33163964 - Received response [200 Command okay]
    System.Net Information: 0 : [3404] FtpControlStream#33163964 - Sending command [PORT 192,168,2,2,7,152]
    System.Net Information: 0 : [3404] FtpControlStream#33163964 - Received response [200 Command okay]
    System.Net Information: 0 : [3404] FtpControlStream#33163964 - Sending command [STOR Shri1.wmv]
    System.Net Information: 0 : [3404] FtpControlStream#33163964 - Received response [150 Status OK, about to open data connection.]
    System.Net Verbose: 0 : [3404] Exiting FtpWebRequest#28068188::GetRequestStream()
    System.Net Information: 0 : [3404] FtpWebRequest#28068188::(Releasing FTP connection#33163964.)

  • Thursday, August 17, 2006 7:20 PMMariya Atanasova [NCL]MSFT, ModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    Looks like in the second case we never received the 220 and then we block releasing the connection. I'll investigate further and get back to you. Meanwhile, can you tell me if you have this problem in passive mode as well (set UsePassive to true instead of to false).

    Also is your ftp server accessible from outside? Can I test on it? It will be easier to repro since we don't have AksTech FTP

    Mariya

  • Friday, August 18, 2006 9:54 AMkarande23 Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Thanks for giving lot of your time....
    I will mail you regarding the ftp server.

    Tried with UsePassive = true but it gives an error stating "The sever returned an address in response to PASV that is different than the address to which ftp connection was made"

    Thanks again.




  • Saturday, August 19, 2006 12:40 AMMariya Atanasova [NCL]MSFT, ModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    Got your e-mail and I am in the process of investigating the issue. I'll get back to you on Monday.

    Thanks
    Mariya

  • Monday, August 21, 2006 4:36 PMMariya Atanasova [NCL]MSFT, ModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer

    This code worked for me and I didn't get any exceptions. I tried with a simple txt file in the current directory. Note: since you're using active ftp you have to set the proxy to null.  

    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Net;
    using System.IO;

    namespace FtpWebRequest_MSDN2
    {
    class Program
    {
    static void Main(string[] args)
    {
    string serverUri = ftp://yourIP/textFile.txt;
    FtpWebRequest request = (FtpWebRequest)WebRequest.Create(serverUri);
    request.UsePassive =
    false;
    request.Method =
    WebRequestMethods.Ftp.UploadFile;
    request.Credentials =
    new NetworkCredential("user", "pass");
    request.Proxy =
    null;
    Stream requestStream = request.GetRequestStream();
    const int bufferLength = 2048;
    byte[] buffer = new byte[bufferLength];
    int count = 0;
    int readBytes = 0;
    FileStream stream = File.OpenRead("textFile.txt");
    do
    {
    readBytes = stream.Read(buffer, 0, bufferLength);
    requestStream.Write(buffer, 0, readBytes);
    count += readBytes;
    }
    while (readBytes != 0);
    requestStream.Close();
    FtpWebResponse response = (FtpWebResponse)request.GetResponse();
    Stream respStream = response.GetResponseStream();
    respStream.Close();
    response.Close();
    }
    }

    This is just a sample without try-catch and any other checks to get you started. I've used as much of your code as I could to make it easier for you to find the problem. 90% of the time the problem with  active ftp connections is that your firewall or antivirus software block the response from the server and when we don't receive it we consider the connection didn't succeed and throw an exception.

    Mariya

  • Tuesday, August 22, 2006 8:38 AMkarande23 Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Lets see how this goes...but wud have to make some changes because this did break again at same place.. 
  • Tuesday, August 22, 2006 5:03 PMMariya Atanasova [NCL]MSFT, ModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Minor detail: to simplify things you can read the whole file at one go with StreamReader.ReadToEnd.
  • Saturday, December 15, 2007 6:57 PMEduardo Coelho Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    Hi,

     

    This also happens to me but only for large files (like >20MB).

     

    Did you get any fix on this?

     

    Regards,

     

    Eduardo

  • Tuesday, August 26, 2008 9:07 PMjames_sciosoft.ca Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    The trace looks like you are using a numeric IP (71.6.135.39).

    But if you are using a string name in serverUri, try using the numeric 71.6.135.39 and try toggling passive mode.

    This happens sometimes, particularily with shared hosting.

    James

  • Friday, October 30, 2009 7:21 AMOma Danny Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    I'm not sure why it happens, but handling the FTP is quite different from server to server. I had an headache when developing my own FTP lib using .NET socket & FTPWebrequest, finally I decided to use this .NET FTP Component .