I am upload large tar files about 10GB to the ftp server. It is taking a lot of time and throws the following error: Unable to write data to the transport connection: An existing connection was forcibly closed by the remote host.
I have referred to the following:
Why is my ftp upload method so slow?
https://www.codeproject.com/Questions/204070/how-to-write-c-code-to-increase-the-ftp-file-uploa
The following is the code:
FileStream fs = null;
Stream rs = null;
try
{
string file = args[0].Replace("---"," ");
string ftpServer = args[1].ToString();
string uploadFileName = new FileInfo(file).Name;
string uploadUrl = ftpServer;
Console.WriteLine("Start Time: {0}", DateTime.Now);
Console.WriteLine("File Name: {0}",file);
fs = new FileStream(file, FileMode.Open, FileAccess.Read);
string ftpUrl = string.Format("{0}/{1}", uploadUrl, uploadFileName);
FtpWebRequest requestObj = FtpWebRequest.Create(ftpUrl) as FtpWebRequest;
requestObj.Timeout = -1; // <---- -1 is Infinite
requestObj.Method = WebRequestMethods.Ftp.UploadFile;
rs = requestObj.GetRequestStream();
byte[] buffer = new byte[4096];
//byte[] buffer = new byte[16000]; // <--- 16k
int read = 0;
while ((read = fs.Read(buffer, 0, buffer.Length)) != 0)
{
rs.Write(buffer, 0, read);
}
rs.Flush();
}
catch (Exception ex)
{
Console.WriteLine("File upload/transfer Failed.\r\nError Message:\r\n" + ex.Message);
}
finally
{
if (fs != null)
{
fs.Close();
fs.Dispose();
}
if (rs != null)
{
rs.Close();
rs.Dispose();
}
}
Console.WriteLine("End Time: {0}", DateTime.Now);
Console.WriteLine("Exiting the application.. press any key to continue");
Console.ReadLine();
UPDATE: I am uploading the file to FTP DropBox and No access to the logs. While trying to upload via clients like FileZilla the speed is faster. The limit of the dropbox is 300 GB. Is it possible to find the rate of transfer?
Please explain the solution as well for my understanding as I am a beginner. Thanks in advance.