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

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

Thanks


        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();

            try
            {

                // 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

                strm.Close();

                fs.Close();

            }

            catch (Exception ex)
            {

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

            }

        }

Open in new window

Lisa BAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

käµfm³d 👽Commented:
When you build the URI for the upload, it needs to include the filename, not simply the directory where you're going to put the file. This may be why you are receiving the error. Try including the filename also:

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

Open in new window

0
Lisa BAuthor Commented:
Hi

I have changed the code to

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 + "/" + fileInf.Name));

reqFTP.Method = WebRequestMethods.Ftp.UploadFile;

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

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

Open in new window


And I am still getting the same error.  The line below

FileInfo fileInf = new FileInfo(filename);

filename has the full location of the file on my pc is that correct?
0
Lisa BAuthor Commented:
Hi

Was able to get the problem solved using the code below

        public void SendFile(string filename)
        {

            string ftpServerIP = "ftp://ftp.test.org";
            string ftpUserID = "Test";
            string ftpPassword = "1234";
            string destinationPath = @"/incoming/";
            FileInfo fileInf = new FileInfo(filename);

            try
            {

                FtpWebRequest request = (FtpWebRequest)WebRequest.Create(ftpServerIP + destinationPath + Path.GetFileName(filename));
                request.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
                request.Method = WebRequestMethods.Ftp.UploadFile;

                request.UseBinary = false;
                request.UsePassive = false;

                request.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();
                try
                {
                    // Stream to which the file to be upload is written
                    Stream strm = request.GetRequestStream();
                    // 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
                    strm.Close();
                    fs.Close();
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message, "Upload Error");
                }


            }
            catch (Exception Ex)
            {
                Console.WriteLine(Ex.ToString());
                Console.ReadKey(false);
            }
        }

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Lisa BAuthor Commented:
No other solutions provided - was able to get this working after several days using the code in my comment above
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.