我已经看到了其他帖子,但我读过的帖子中没有一个答案看起来真有效。所以这些帖子没有帮助。我已经尝试了所有我见过的建议 简而言之:此例程适用于中小型文件。但是一旦我达到大约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();
}
}