hi all,
i received error:
" The underlying connection was closed: An unexpected error occurred on a receive"
while uploading large files (ie 100MB) from one server to another I found file copying at destination
but at the time of below statement I gor error:
reqStream.Close();
and also suggest why after copying file it take long time to close request stream and give above Error plz help.
my code snipet is
public void UploadFile(string _FileName, string _UploadPath, string _FTPUser, string _FTPPass)
{
try
{
// Get the local file name: C:\Users\Rhyous\Desktop\File1.zip
// and get just the filename: File1.zip. This is so we can add it
// to the full URI.
String filename = Path.GetFileName(_FileName);
// Open a request using the full URI, c/file.ext
FtpWebRequest request = (FtpWebRequest)FtpWebRequest.Create(_UploadPath);
// Configure the connection request
request.Method = WebRequestMethods.Ftp.UploadFile;
request.Credentials = new NetworkCredential(_FTPUser, _FTPPass);
request.UsePassive = true;
request.UseBinary = true;
request.KeepAlive = false;
// Create a stream from the file
FileStream stream = File.OpenRead(_FileName);
byte[] buffer = new byte[stream.Length];
// Read the file into the a local stream
stream.Read(buffer, 0, buffer.Length);
// Close the local stream
stream.Close();
// Create a stream to the FTP server
Stream reqStream = request.GetRequestStream();
// Write the local stream to the FTP stream
// 1MB bytes at a time
int offset = 0;
int chunk = (buffer.Length > 1048576) ? 1048576: buffer.Length;
while (offset < buffer.Length)
{
reqStream.Write(buffer, offset, chunk);
offset += chunk;
chunk = (buffer.Length - offset < chunk) ? (buffer.Length - offset) : chunk;
}
// Close the stream to the FTP server
reqStream.Close();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message, "Upload Error");
}
}
Loading

Mai Hu NaPosted Mar 19, 2013, 4:05 AM
also i do not want wait long time after copying file from one server to another server
want close as soon as file upload completed Dynamically.
not want hard coded like this:
Jignesh TrivediPosted Mar 19, 2013, 12:31 AM
hi,
I think, this problem is occured because of the default value for the maxRequestLength parameter in the section of the Machine.config file is 4096 (4 megabytes).
try..
also check is it work for small size file?
hope this will help you.