我有一个FTP(Filezila服务器),我想使用c#下载一个大文件。
WebClient client = new WebClient();
client.Credentials = new NetworkCredential(userName, password);
client.DownloadFile(new Uri("ftp://XXX.XXX.XXX.XXX/" + fileName), destinationFileFullPath);
或
FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://XXX.XXX.XXX.XXX/" + fileName);
request.Method = WebRequestMethods.Ftp.DownloadFile;
request.KeepAlive = true; // I tried both
request.UseBinary = true; // I tried both
request.UsePassive = true;
request.Credentials = new NetworkCredential(userName, password);
FtpWebResponse response = (FtpWebResponse)request.GetResponse();
Stream responseStream = response.GetResponseStream();
// Writing bytes to local files
using (var outputStream = File.OpenWrite(destinationFileFullPath))
{
byte[] chunk = new byte[2048];
int bytesRead;
do
{
bytesRead = responseStream.Read(chunk, 0, chunk.Length);
outputStream.Write(chunk, 0, bytesRead);
} while (bytesRead > 0);
outputStream.Flush();
}
responseStream.Close();
response.Close();
对于小文件,它运行良好,但对于大文件(大的意思是大约300MB以上)它可以工作,但在传输结束时(我在filezilla服务器端看到传输成功)由于未知原因我的应用程序只是挂在行
client.DownloadFile(new Uri("ftp://XXX.XXX.XXX.XXX/" + fileName), destinationFileFullPath);
或
bytesRead = responseStream.Read(chunk, 0, chunk.Length);
直到我收到超时异常。如果我强行休息(暂停),我可以看到它真的停留在这条线上。就像Filezilla关闭连接而C#只是等待更多阅读... 你有没有遇到过这个问题?
感谢您的帮助。