질문하기질문하기
 

답변됨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일 월요일 오후 4: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일 목요일 오후 3: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일 월요일 오후 4: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일 수요일 오후 4: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일 목요일 오후 7: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일 토요일 오전 12: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일 월요일 오후 4: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일 화요일 오후 5:03Mariya Atanasova [NCL]MSFT, 중재자사용자 메달사용자 메달사용자 메달사용자 메달사용자 메달
     
    Minor detail: to simplify things you can read the whole file at one go with StreamReader.ReadToEnd.
  • 2007년 12월 15일 토요일 오후 6: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일 화요일 오후 9: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 .