Click here to Skip to main content
15,887,683 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi, I am trying to upload files from one server to another
Everything works fine with small files(< 30MB), but when I try to upload a large file, an error appears "The connection was closed: An unexpected error occurred on a receive"

this is my code:

C#
try
        {
            string dir = ConfigurationManager.AppSettings["ftp"].ToString();
            string user = ConfigurationManager.AppSettings["ftpUser"].ToString();
            string pass = ConfigurationManager.AppSettings["ftpPass"].ToString();

            //SUBIR

            DirectoryInfo directory = new DirectoryInfo(Server.MapPath("/documentos/" + clave + "/"));
            FileInfo[] files = directory.GetFiles("*.pdf");
			
			FtpWebRequest requestFTPUploader = (FtpWebRequest)WebRequest.Create(dir + "/" + "PANDEMO" + clave + "/");
            requestFTPUploader.Credentials = new NetworkCredential(user, pass);
            requestFTPUploader.Method = WebRequestMethods.Ftp.MakeDirectory;
            StreamReader reader = new StreamReader(requestFTPUploader.GetResponse().GetResponseStream());
            
            for (int i = 0; i < files.Length; i++)
            {
                requestFTPUploader = (FtpWebRequest)WebRequest.Create(dir + "/" + "PANDEMO" + clave + "/" + files[i].Name);
                requestFTPUploader.Credentials = new NetworkCredential(user, pass);
                requestFTPUploader.UsePassive = true;
                requestFTPUploader.UseBinary = true;
                requestFTPUploader.KeepAlive = true;
                requestFTPUploader.Method = WebRequestMethods.Ftp.UploadFile;
                requestFTPUploader.Timeout = -1;

                reader = new StreamReader(requestFTPUploader.GetResponse().GetResponseStream());

                FileInfo fileInfo = new FileInfo(Server.MapPath("/documentos/" + clave + "/" + files[i].Name));
                FileStream fileStream = fileInfo.OpenRead();

                int bufferLength = 2048;
                byte[] buffer = new byte[bufferLength];

                Stream uploadStream = requestFTPUploader.GetRequestStream();
                int contentLength = fileStream.Read(buffer, 0, bufferLength);
                uploadStream.WriteTimeout = -1;
                uploadStream.ReadTimeout = -1;
                while (contentLength != 0)
                {
                    uploadStream.Write(buffer, 0, contentLength);
                    contentLength = fileStream.Read(buffer, 0, bufferLength);
                }
                uploadStream.Close(); 
                fileStream.Close();

                

                requestFTPUploader = null;
            }
        }
        catch (Exception err)
        {
            ShowMssg(err.Message);

        }


also, in the web.config I used this:
HTML
<security>
      <requestFiltering>
        <requestLimits maxAllowedContentLength="314572800"/>
      </requestFiltering>
    </security>
<system.web>
    <httpRuntime maxRequestLength="307200" executionTimeout="100000"/>
...


Does someone knows why Only happens with large files?
Posted
Comments
CHill60 9-Jul-14 19:22pm    
Timeout perhaps?
memd24 10-Jul-14 10:13am    
I added .TimeOut = -1, before I used .timeOut = 1000000000, but nothing, the same issure. Can affect the IIS in the ftp server?
UPDATE
I discovered that the issure occurs in the
uploadStream.Close();
When I try to sent a 140mb file from the first server to the second, it finish the the while code, and when it go to close it, it goes to the catch line and show the error "An unexpected error occurred on a receive"

1 solution

Since its a large file the OS will send it in chunks. Probably there is still some bytes in the buffer when you go to close it. Try uploadStream.Flush() before closing the stream to write the last of the bytes to the stream first.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900