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:
try
{
string dir = ConfigurationManager.AppSettings["ftp"].ToString();
string user = ConfigurationManager.AppSettings["ftpUser"].ToString();
string pass = ConfigurationManager.AppSettings["ftpPass"].ToString();
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:
<security>
<requestFiltering>
<requestLimits maxAllowedContentLength="314572800"/>
</requestFiltering>
</security>
<system.web>
<httpRuntime maxRequestLength="307200" executionTimeout="100000"/>
...
Does someone knows why Only happens with large files?