首页 > 解决方案 > C# FTP 挂在大文件上

问题描述

我看过其他帖子,但我读过的帖子都没有一个真正有效的答案。所以这些帖子没有帮助。我已经尝试了我看到的每一个建议。
简而言之:此例程非常适合中小型文件。但是一旦我达到大约 1 GB,它就会挂起。任何帮助是极大的赞赏。

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();
    }
}

标签: c#ftp

解决方案


推荐阅读