locked
Ftp Upload File

    Question

  • 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.
    Thursday, August 10, 2006 7:39 AM

Answers

  • 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

    Monday, August 21, 2006 4:36 PM
    Moderator

All replies

  • 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

    Thursday, August 10, 2006 3:07 PM
    Moderator
  • 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.
    Saturday, August 12, 2006 5:46 AM
  • 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

    Monday, August 14, 2006 4:28 PM
    Moderator
  • 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

    Tuesday, August 15, 2006 2:12 AM
    Moderator
  • 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 5:27 AM
  • 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 6:21 AM
  • 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

    Wednesday, August 16, 2006 4:24 PM
    Moderator
  • 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:36 AM
  • 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 6:41 AM
  • 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:14 AM
  • 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

    Thursday, August 17, 2006 7:20 PM
    Moderator
  • 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.




    Friday, August 18, 2006 9:54 AM
  • Got your e-mail and I am in the process of investigating the issue. I'll get back to you on Monday.

    Thanks
    Mariya

    Saturday, August 19, 2006 12:40 AM
    Moderator
  • 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

    Monday, August 21, 2006 4:36 PM
    Moderator
  • Lets see how this goes...but wud have to make some changes because this did break again at same place.. 
    Tuesday, August 22, 2006 8:38 AM
  • Minor detail: to simplify things you can read the whole file at one go with StreamReader.ReadToEnd.
    Tuesday, August 22, 2006 5:03 PM
    Moderator
  • Hi,

     

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

     

    Did you get any fix on this?

     

    Regards,

     

    Eduardo

    Saturday, December 15, 2007 6:57 PM
  • 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

    Tuesday, August 26, 2008 9:07 PM
  • Good afternoon Mariya,
    I want to ask you about upload file into the FTP server.
    The code you gave is very good and worked fine, but I found it does not work on .Net 1.1
    Do you have any function that can work on Framework .Net 1.1?

    I do appreciate very much your help

    Thank you very much

    Maan
    Wednesday, December 16, 2009 5:46 PM
  •  
    • Proposed as answer by Dharuman Monday, January 04, 2010 7:41 PM
    • Edited by Dharuman Monday, January 04, 2010 7:44 PM
    Tuesday, December 22, 2009 10:39 PM
  • Hi,
    Any can please reply my issues?
    Monday, December 28, 2009 3:45 PM
  • Hallo Maria,

    I coded for the FTP upload file, i am getting error message as The underlying connection was closed: The server committed a protocol violation.

    My code as follows

    FtpWebRequest

    request = null;
    FtpWebResponse response = null;

     

    FtpWebRequest) WebRequest.Create(FTPServerPath+Filename);
    request.UsePassive = UsePassive;
    request.Method =
    WebRequestMethods.Ftp.UploadFile ;
    request.Credentials =
    new NetworkCredential(Username, Password);
    request.Proxy =
    null;

     

    StreamReader streamReader = new StreamReader(UploadFileNamePath);
    Byte[] fileContent = Encoding.UTF8.GetBytes(streamReader.ReadToEnd());
    streamReader.Close();
    streamReader.Dispose();

     

     

    Stream requestStream = request.GetRequestStream();
    requestStream.Write(fileContent, 0, fileContent.Length);
    requestStream.Close();
    requestStream.Dispose();

    response = (

     

    FtpWebResponse) request.GetResponse()

    Also please see my trace log information

    System.Net Verbose: 0 : [4128] WebRequest::Create(ftp://ftp.example.com/ErrorPage.doc)
    System.Net Information: 0 : [4128] FtpWebRequest#49907388::.ctor(ftp://ftp.example.com/ErrorPage.doc)
    System.Net Verbose: 0 : [4128] Exiting WebRequest::Create()  -> FtpWebRequest#49907388
    System.Net Verbose: 0 : [4128] FtpWebRequest#49907388::GetRequestStream()
    System.Net Information: 0 : [4128] FtpWebRequest#49907388::GetRequestStream(Method=STOR.)
    System.Net.Sockets Verbose: 0 : [4128] Socket#31239962::Socket(InterNetwork#2)
    System.Net.Sockets Verbose: 0 : [4128] Exiting Socket#31239962::Socket()
    System.Net.Sockets Verbose: 0 : [4128] Socket#31239962::Connect(115:21#1933695062)
    System.Net.Sockets Verbose: 0 : [4128] Exiting Socket#31239962::Connect()
    System.Net Information: 0 : [4128] Associating FtpWebRequest#49907388 with FtpControlStream#63621045
    System.Net.Sockets Verbose: 0 : [4128] Socket#31239962::Receive()
    System.Net.Sockets Verbose: 0 : [4128] Data from Socket#31239962::Receive
    System.Net.Sockets Verbose: 0 : [4128] 00000000 :                                                 :
    System.Net.Sockets Verbose: 0 : [4128] Exiting Socket#31239962::Receive()  -> 0#0
    System.Net.Sockets Verbose: 0 : [4128] Socket#31239962::Dispose()
    System.Net Information: 0 : [4128] FtpWebRequest#49907388::(Releasing FTP connection#63621045.)

    System.Net Error: 0 : [4128] Exception in the FtpWebRequest#49907388::GetRequestStream - The underlying connection was closed: The server committed a protocol violation.
    System.Net Error: 0 : [4128]    at System.Net.FtpWebRequest.SyncRequestCallback(Object obj)
       at System.Net.FtpWebRequest.RequestCallback(Object obj)
       at System.Net.CommandStream.InvokeRequestCallback(Object obj)
       at System.Net.CommandStream.Abort(Exception e)
       at System.Net.FtpWebRequest.FinishRequestStage(RequestStage stage)
       at System.Net.FtpWebRequest.GetRequestStream()
    System.Net Error: 0 : [4128] Exception in the
    #28589617::UnhandledExceptionHandler - The underlying connection was closed: The server committed a protocol violation.
    System.Net Error: 0 : [4128]    at System.Net.FtpWebRequest.SyncRequestCallback(Object obj)
       at System.Net.FtpWebRequest.RequestCallback(Object obj)
       at System.Net.CommandStream.InvokeRequestCallback(Object obj)
       at System.Net.CommandStream.Abort(Exception e)
       at System.Net.FtpWebRequest.FinishRequestStage(RequestStage stage)
       at System.Net.FtpWebRequest.GetRequestStream()
       at FileTransfer.FileTransferCls.FTPUploadFile(String Filename, String UploadFileNamePath) in C:\healthsmt\WindowsApp\FileTransfer\FileTransfer\FileTransferCls.cs:line 130
       at FileTransfer.TransferFile.btnUploadFileToFTP_Click(Object sender, EventArgs e) in C:\healthsmt\WindowsApp\FileTransfer\FileTransfer\TransferFile.cs:line 156
       at System.Windows.Forms.Control.OnClick(EventArgs e)
       at System.Windows.Forms.Button.OnClick(EventArgs e)
       at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
       at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
       at System.Windows.Forms.Control.WndProc(Message& m)
       at System.Windows.Forms.ButtonBase.WndProc(Message& m)
       at System.Windows.Forms.Button.WndProc(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
       at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
       at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
       at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData)
       at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
       at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
       at System.Windows.Forms.Application.Run(Form mainForm)
       at FileTransfer.Program.Main() in C:\healthsmt\WindowsApp\FileTransfer\FileTransfer\Program.cs:line 17
       at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
       at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()
    System.Net Verbose: 0 : [4128] Exiting FtpWebRequest#49907388::GetRequestStream()

    Please suggest me to fix this issue and thank you for your effort and time.


    request.EnableSsl = EnableSSL;
    request.UseBinary = UseBinary;
    request.KeepAlive = KeepAlive;

     

     

     

    request= (


    I am sorry, by mistake i clicked Propose as answer, so this is my question please anyone answer me..
    Monday, January 04, 2010 7:44 PM
  • Hello,

    I am trying to FTP a .xlsx file to an FTP page. My code will upload a the proper data in need in the file however it uploads it as a zipped folder. I have attached my code below. Is there anything I can change in my code to make it upload the .xlsx format of the file. I tried changing the use of Passive to both use and then I tried not using it but I get the same result.

    Any ideas would be greatly appreciated.

    Thank you

    Private Sub FTP(ByVal Plant)
            Dim UpLoadRequest As System.Net.FtpWebRequest = _
            DirectCast(System.Net.WebRequest.Create _
                      ("ftp://XX-XXX-XXX" & Plant & ".xlsx"), System.Net.FtpWebRequest)
            With UpLoadRequest
                .Credentials = New System.Net.NetworkCredential("user", "pas")
                .Proxy = Nothing
                .Method = System.Net.WebRequestMethods.Ftp.UploadFile
                .UsePassive = False
    
            End With
    
            Call Pause(4)
    
            Dim bFile() As Byte = System.IO.File.ReadAllBytes _
            ("C:\Documents and Settings\Desktop\VB Project\" & Plant & ".xlsx")
    
            ' upload file...
            Dim UpLoadStream As System.IO.Stream = _
            UpLoadRequest.GetRequestStream()
    
            With UpLoadStream
                .Write(bFile, 0, bFile.Length)
                .Flush()
                .Close()
                .Dispose()
            End With
    
            Dim reply As Net.FtpWebResponse = UpLoadRequest.GetResponse
            MessageBox.Show(reply.StatusCode.ToString & vbCrLf & reply.StatusDescription)           'JUST FOR TESTING
        End Sub
    Friday, March 05, 2010 1:41 PM
  • An xlsx file is actually just a zip archive with a different extension. Your FTP server is just being too smart for its own good.
    Tuesday, March 09, 2010 8:53 PM
  • 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.

    I sometime have the same problem with MS FTP Server. Many friends of mine have moved to FileZilla FTP server.

    Regarding your issue, there should be some delay between the Flush and Close methods.

    Tuesday, September 28, 2010 7:20 AM
  • 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


    Something error occurs when opening the link, What's the problem?
    Thursday, October 07, 2010 11:13 PM