1

I am currently trying to download a large file (1.13gb) from a ftp location and it is timing out.

When I checked the download was only doing about 350mb every 10 mins on the server. Other than increasing the request.Timeout to almost an hour (!!) is there a better way to download and stream large files like this.

Here is the code I am currently using:

private void DownloadFromFtp(IFeedProviderConfiguration configuration, string urlData)
{
    FtpWebRequest request = (FtpWebRequest)WebRequest.Create(urlData);
    if (!string.IsNullOrEmpty(configuration.Username))
    {
        request.Credentials = new NetworkCredential(configuration.Username, configuration.Password);
    }
    request.ReadWriteTimeout = 600000;
    request.Timeout = 600000;
    request.UseBinary = true;
    request.Method = WebRequestMethods.Ftp.DownloadFile;

    FtpWebResponse response = (FtpWebResponse)request.GetResponse();
    var stream = GetUnCompressFromStream(urlData, response.GetResponseStream());
    /*StreamReader reader = new StreamReader(responseStream);
    documentContent = reader.ReadToEnd();
    reader.Close();*/

    this.WriteToFile(stream, this._localFile);
    stream.Close();
}
Wai Ha Lee
  • 8,598
  • 83
  • 57
  • 92
anna
  • 1,001
  • 6
  • 23
  • 40
  • Just do not set any timeout (use Infinite). You actually don't know how long it will take. However you should remove ReadToEnd() and perform a block by block read, an external thread (you wouldn't perform this check within your download thread) you may manually check progress. If after - let's say - 60 seconds you didn't receive any new data then you may cancel download. – Adriano Repetti Oct 07 '15 at 10:33
  • 1
    You can try doing it with retries. Example is here: http://stackoverflow.com/questions/11950211/downloading-large-files150mb-from-ftp-server-hangs – saniokazzz Oct 07 '15 at 10:34
  • Adriano it seems the default timeout is 100000 (just over a minute) so could set timeout = Timeout.Infinite just a bit concerned with the implications (if any) of doing this. – anna Oct 07 '15 at 10:52

0 Answers0