提出问题提出问题
 

已答复Ftp Upload File

  • 2006年8月10日 7:39karande23 用户奖牌用户奖牌用户奖牌用户奖牌用户奖牌
     
    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.

答案

  • 2006年8月21日 16:36Mariya Atanasova [NCL]MSFT, 版主用户奖牌用户奖牌用户奖牌用户奖牌用户奖牌
     已答复

    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

全部回复

  • 2006年8月10日 15:07Mariya Atanasova [NCL]MSFT, 版主用户奖牌用户奖牌用户奖牌用户奖牌用户奖牌
     

    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

  • 2006年8月12日 5:46karande23 用户奖牌用户奖牌用户奖牌用户奖牌用户奖牌
     
    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.
  • 2006年8月14日 16:28Mariya Atanasova [NCL]MSFT, 版主用户奖牌用户奖牌用户奖牌用户奖牌用户奖牌
     

    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

  • 2006年8月15日 2:12Mariya Atanasova [NCL]MSFT, 版主用户奖牌用户奖牌用户奖牌用户奖牌用户奖牌
     

    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

  • 2006年8月16日 5:27karande23 用户奖牌用户奖牌用户奖牌用户奖牌用户奖牌
     
    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...
  • 2006年8月16日 6:21karande23 用户奖牌用户奖牌用户奖牌用户奖牌用户奖牌
     
    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.)

  • 2006年8月16日 16:24Mariya Atanasova [NCL]MSFT, 版主用户奖牌用户奖牌用户奖牌用户奖牌用户奖牌
     

    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

  • 2006年8月17日 6:36karande23 用户奖牌用户奖牌用户奖牌用户奖牌用户奖牌
     
    The last line in the Trace says Releasing....it is not released?
    There is no mention 220 command which states Transfer complete.
  • 2006年8月17日 6:41karande23 用户奖牌用户奖牌用户奖牌用户奖牌用户奖牌
     
    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()

  • 2006年8月17日 7:14karande23 用户奖牌用户奖牌用户奖牌用户奖牌用户奖牌
     
    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.)

  • 2006年8月17日 19:20Mariya Atanasova [NCL]MSFT, 版主用户奖牌用户奖牌用户奖牌用户奖牌用户奖牌
     

    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

  • 2006年8月18日 9:54karande23 用户奖牌用户奖牌用户奖牌用户奖牌用户奖牌
     
    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.




  • 2006年8月19日 0:40Mariya Atanasova [NCL]MSFT, 版主用户奖牌用户奖牌用户奖牌用户奖牌用户奖牌
     

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

    Thanks
    Mariya

  • 2006年8月21日 16:36Mariya Atanasova [NCL]MSFT, 版主用户奖牌用户奖牌用户奖牌用户奖牌用户奖牌
     已答复

    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

  • 2006年8月22日 8:38karande23 用户奖牌用户奖牌用户奖牌用户奖牌用户奖牌
     
    Lets see how this goes...but wud have to make some changes because this did break again at same place.. 
  • 2006年8月22日 17:03Mariya Atanasova [NCL]MSFT, 版主用户奖牌用户奖牌用户奖牌用户奖牌用户奖牌
     
    Minor detail: to simplify things you can read the whole file at one go with StreamReader.ReadToEnd.
  • 2007年12月15日 18:57Eduardo Coelho 用户奖牌用户奖牌用户奖牌用户奖牌用户奖牌
     

    Hi,

     

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

     

    Did you get any fix on this?

     

    Regards,

     

    Eduardo

  • 2008年8月26日 21:07james_sciosoft.ca 用户奖牌用户奖牌用户奖牌用户奖牌用户奖牌
     

    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

  • 2009年10月30日 7:21Oma Danny 用户奖牌用户奖牌用户奖牌用户奖牌用户奖牌
     
    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 .