0

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:

C# : FTP upload buffer size

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.

Tango
  • 386
  • 1
  • 6
  • 29
  • A small file does work? Also consider using `fs.CopyTo(rs);` instead of reading and writing to a buffer on your own. And you might want to dispose your streams as well. – rene Jan 17 '18 at 12:56
  • The small files work fine. The issue is with the big ones. I want to upload the file on the FTP as fast as possible with negligible resources. – Tango Jan 17 '18 at 13:24
  • After how much time does it fail? Do you have access to the ftpserver logs? Any clue in there why it stopped? – rene Jan 17 '18 at 13:43
  • It took around 35 minutes when it failed after transferring about 400mb of 10gb file – Tango Jan 17 '18 at 13:44
  • does it always fail after 35 minutes? – rene Jan 17 '18 at 13:45
  • So what does the other side say (logs)? There may be limits in place, or you´re running out of storage space... – C. Gonzalez Jan 17 '18 at 13:49
  • @C.Gonzalez I don't have access to logs as it is a dropbox. The upload is much quicker with clients like FileZilla also I was able to upload total of 300gb (Similar dropbox where I am trying to upload using the code). I cannot use 3rd part utility for this. Is there a way to identify the transfer speed or transfer rate ? – Tango Jan 17 '18 at 14:02
  • You could try to use a FtpWebResponse with [GetResonse](https://msdn.microsoft.com/EN-US/library/e84yxb88(v=VS.110,d=hv.2).aspx) at the end to at least see if the server shows some additional details as to the reason of failure. – C. Gonzalez Jan 17 '18 at 14:45

0 Answers0