We help IT Professionals succeed at work.
Get Started

FtpWebRequest - The underlying connection as closed: An unexpected error has occurred on a receive

Lisa B
Lisa B asked
Last Modified: 2015-07-11
Hi Everyone

I am trying to upload a csv which at most has about 15 records to a ftp site but I am getting the error - The underlying connection as closed: An unexpected error occurred on a receive.

Does anyone have any suggestions on how to fix this error?

Using visual studio 2010


        public void SendFile(string filename)

            string ftpServerIP = "ftp.test.org";
            string ftpUserID = "Test";
            string ftpPassword = "1234";
            string ftpDirectory = "incoming";

            FileInfo fileInf = new FileInfo(filename);

            string uri = "ftp://" + ftpServerIP + "/" + ftpDirectory;

            FtpWebRequest reqFTP;

            reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + ftpServerIP + "/" + ftpDirectory));

            reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);

            reqFTP.UsePassive = true;
            reqFTP.UseBinary = true;
            reqFTP.KeepAlive = false;

            reqFTP.Method = WebRequestMethods.Ftp.UploadFile;

            reqFTP.Timeout = 10000000;

            reqFTP.ReadWriteTimeout = 10000000;

            reqFTP.ContentLength = fileInf.Length;

            // The buffer size is set to 2kb
            int buffLength = 2048;

            byte[] buff = new byte[buffLength];

            int contentLen;

            // Opens a file stream (System.IO.FileStream) to read the file to be uploaded

            FileStream fs = fileInf.OpenRead();


                // Stream to which the file to be upload is written

                Stream strm = reqFTP.GetRequestStream();

                strm.ReadTimeout = 9999999;
                strm.WriteTimeout = 9999999;

                // Read from the file stream 2kb at a time

                contentLen = fs.Read(buff, 0, buffLength);

                // Until Stream content ends

                while (contentLen != 0)

                    // Write Content from the file stream to the FTP Upload Stream

                    strm.Write(buff, 0, contentLen);

                    contentLen = fs.Read(buff, 0, buffLength);


                // Close the file stream and the Request Stream




            catch (Exception ex)

                Console.WriteLine(ex.Message, "Upload Error");



Open in new window

Watch Question
This problem has been solved!
Unlock 1 Answer and 4 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE