1

I have seen other posts on this, but none of the posts that I have read have an answer that really seems to work. So those posts do not help. I've tried every suggestion that I've seen.
In short: This routine works perfect for small and medium files. But once I get up to about 1 GB, it hangs. Any help is greatly appreciated.

System.Net.ServicePointManager.Expect100Continue = false;
System.Diagnostics.Trace.WriteLine("FTP: Download " + this._URL + fixURL(this._Folder).Replace("\\", "/") + fixURL(ftpFileName));
FtpWebRequest ftpRequest = (FtpWebRequest)WebRequest.Create(this._URL + fixURL(this._Folder).Replace("\\", "/") + fixURL(ftpFileName));
ftpRequest.Credentials = new NetworkCredential(this._UserName, this._Password);
ftpRequest.UsePassive = false;
ftpRequest.KeepAlive = true;
ftpRequest.UseBinary = true;
ftpRequest.Timeout = -1;
ftpRequest.ReadWriteTimeout = 1000 * 60 * 60 * 5;
ftpRequest.ServicePoint.ConnectionLimit = 1000;
ftpRequest.Method = WebRequestMethods.Ftp.DownloadFile;
using (FtpWebResponse ftpResponse = (FtpWebResponse)ftpRequest.GetResponse())
{
    using (Stream ftpStream = ftpResponse.GetResponseStream())
    {
        ftpStream.ReadTimeout = 1000 * 60 * 60 * 5;
        using (FileStream fileStream = File.Create(outFolder + @"\" + ftpFileName))
        {
            Byte[] buffer = new Byte[8092];
            Int32 bytesRead = ftpStream.Read(buffer, 0, buffer.Length);
            Int64 bytessofar = bytesRead;
            System.Diagnostics.Trace.WriteLine("FTP: Download Read Block " + bytessofar.ToString("0"));
            while (bytesRead > 0)
            {
                fileStream.Write(buffer, 0, bytesRead);
                bytesRead = ftpStream.Read(buffer, 0, buffer.Length);
                bytessofar += bytesRead;
                System.Diagnostics.Trace.WriteLine("FTP: Download Read Block " + bytessofar.ToString("0"));
            }
        }
        ftpRequest.Abort();
    }
}
Daxtron2
  • 1,239
  • 1
  • 11
  • 19
Brian Kitt
  • 665
  • 1
  • 6
  • 20
  • 1
    Are you sure it's not your *connection* or the *server* that blocks? What does "hangs" mean too? Did you try debugging? Did you try pausing the application when it appears to hand and check what it's doing at that point? Why the call to `Abort` ? – Panagiotis Kanavos May 24 '18 at 15:56
  • On the connection, I download dozens of small/medium files without issue. This routine stops on the ftpStream.Read and just 'hangs' there. I go over to FileZilla on the same machine, same credentials, and download the 1GB file just fine. I run this both in release mode and debug (that's why the Trace.Writeline). It hangs in both modes. The call to .abort was a suggestion by someone else that perhaps I needed a clean break after downloading the small files. – Brian Kitt May 25 '18 at 16:24
  • I said ftp not FileZilla. FileZilla will try to restart if there's a problem and it probably won't try do download the entire file in one go, it will download blocks. Three's *always* a chance that a download will fail and the larger file, the greater the chance simply because it takes longer. Applications like FileZilla are written so they can handle such problems – Panagiotis Kanavos May 28 '18 at 07:07
  • Besides, you still haven't provided any helpful information apart from "it hangs". For all anyone knows you are on a mobile or ISP connection that cuts downloads greater than 1GB. Trying settings at random usually causes more harm than good - why disable PASSIVE for example? Enable [System.Net's tracing](https://learn.microsoft.com/en-us/dotnet/framework/network-programming/network-tracing) instead as show in [How To: Configure Network Tracing](https://learn.microsoft.com/en-us/dotnet/framework/network-programming/how-to-configure-network-tracing) and check what happens – Panagiotis Kanavos May 28 '18 at 07:15
  • I disable passive because it was suggested on another post. Not sure how much clearer I can make 'it hangs'. The ftpStream.Read never returns, not much else I can say about it. I'm on Century Link, not a mobile. It's nothing in my network that's stopping me, that's why I say that when I use FileZilla on the same machine and same file it works fine. That rules out the possibility that it can be an environmental condition of some type. I"m sure it's something quirky in the ftpStream, I just need to figure out what it is. – Brian Kitt May 29 '18 at 07:44
  • Don't assume. Enable network tracing and find out. Trying random suggestions won't work unless you encounter the exact same problem. PASV mode was created to *avoid* problems caused by the server trying to connect directly to the client, something that is usually blocked by ISPs or NAT firewals – Panagiotis Kanavos May 29 '18 at 08:27
  • You should probably try to download the file using `WebClient.DownloadFile` as [shown here](https://stackoverflow.com/questions/13109823/upload-file-and-download-file-from-ftp). This class uses FtpWebRequest too, so if it works you'll know there's something wrong in the code, possibly due to the settings. If you check the linked answer you'll notice that you can use `Stream.CopyTo` to copy to a FileStream directly, no need for the loop. – Panagiotis Kanavos May 29 '18 at 08:32
  • 1
    Thanks for all the great feedback. I changed my routine to retry the ftpStream.Read on a failure (up to 'x' times), moved it to production, and it's been working perfect for a couple of days now. – Brian Kitt Jun 01 '18 at 21:57

0 Answers0