简体   繁体   English

通过WebClient / FTP下载大文件的问题

[英]Issue downloading large files via WebClient / FTP

I'm currently building an application that is, among other things, going to download large files from a FTP server. 我目前正在构建一个应用程序,其中包括从FTP服务器下载大文件。 Everything works fine for small files (< 50 MB) but the files I'm downloading are way bigger, mainly over 2 GB. 一切都适用于小文件(<50 MB),但我正在下载的文件更大,主要超过2 GB。

I've been trying with a Webclient using DownloadfileAsync() and a list system as I'm downloading these files one after the other due to their sizes. 我一直在尝试使用DownloadfileAsync()和列表系统的Webclient,因为我们正在逐个下载这些文件,因为它们的大小。

DownloadClient.DownloadProgressChanged += new DownloadProgressChangedEventHandler(DownloadProgress);
DownloadClient.DownloadFileCompleted += new AsyncCompletedEventHandler(DownloadCompleted);

private void FileDownload()
{
    DownloadClient.DownloadFileAsync(new Uri(@"ftp://" + RemoteAddress + FilesToDownload[0]), LocalDirectory + FilesToDownload[0]));
}

private void DownloadProgress(object sender, DownloadProgressChangedEventArgs e)
{
    // Handle progress
}

private void DownloadCompleted(object sender, AsyncCompletedEventArgs e)
{
    FilesToDownload.RemoveAt(0);
    FileDownload();
}

It works absolutely fine this way on small files, they are all downloaded one by one, the progress is reported and DownloadCompleted fires after each file. 它在小文件上以这种方式工作绝对精确,它们都是逐个下载的,报告进度并在每个文件后触发DownloadCompleted This issue I'm facing with big files is that it launches the first download without any issue but doesn't do anything after that. 我面对大文件的这个问题是它启动了第一次下载而没有任何问题,但之后没有做任何事情。 The DownloadCompleted event never fires for some reasons. 由于某些原因, DownloadCompleted事件永远不会触发。 It looks like the WebClient doesn't know that the file has finished to download, which is an issue as I'm using this event to launch the next download in the FilesToDownload list. 看起来WebClient不知道文件已经完成下载,这是一个问题,因为我正在使用此事件在FilesToDownload列表中启动下一个下载。

I've also tried to do that synchronously using WebClient.DownloadFile and a for loop to cycle through my FilesToDownload list. 我也尝试使用WebClient.DownloadFilefor循环同步执行我的FilesToDownload列表循环。 It downloads the first file correctly and I get an exception when the second download should start: "The underlying connection was closed: An unexpected error occurred on a receive". 它正确下载第一个文件,并在第二次下载开始时出现异常:“底层连接已关闭:接收时发生意外错误”。

Finally, I've tried to go through this via FTP using edtFTPnet but I'm facing download speed issues (ie My download goes full speed with the WebClient and I just get 1/3 of the full speed with edtFTPnet library). 最后,我试图通过FTP使用edtFTPnet来解决这个问题,但是我面临着下载速度问题(即我的下载速度与WebClient完全一致,我只需要使用edtFTPnet库获得全速的1/3)。

Any thoughts? 有什么想法吗? I have to admit that I'm running out of ideas here. 我不得不承认我这里的想法已经不多了。

public string GetRequest(Uri uri, int timeoutMilliseconds)
{
     var request = System.Net.WebRequest.Create(uri);
     request.Timeout = timeoutMilliseconds;
     using (var response = request.GetResponse())
     using (var stream = response.GetResponseStream())
     using (var reader = new System.IO.StreamReader(stream))
     {
         return reader.ReadToEnd();
     }
 } 

Forgot to update this thread but I figured how to sort this out a while ago. 忘了更新这个帖子,但我想了解如何解决这个问题。

The issue was that the Data connection that is opened for a file transfer randomly times out for some reason or is closed by the server before the transfer ends. 问题是为文件传输打开的数据连接由于某种原因随机超时或在传输结束前由服务器关闭。 I haven't been able to figure out why however as there is a load of local and external network interfaces between my computer and the remote server. 然而,由于我的计算机和远程服务器之间存在大量本地和外部网络接口,我无法弄清楚原因。 As it's totally random (ie the transfer works fine for five files in a row, times out for one file, works fine for the following files etc), the issue may be server or network related. 由于它是完全随机的(即传输在连续五个文件中工作正常,对于一个文件超时,对于以下文件等工作正常),问题可能是服务器或网络相关。

I'm now catching any FTP exception raised by the FTP client object during the download and issue a REST command with an offset equals to the position in the data stream where the transfer stopped (ie total bytes amount of the remote file - currently downloaded bytes amount). 我现在正在捕获FTP客户端对象在下载期间引发的任何FTP异常,并发出一个REST命令,其偏移量等于传输停止的数据流中的位置(即远程文件的总字节数 - 当前下载的字节数)量)。 Doing so allows to get the remaining bytes that are missing in the local file. 这样做可以获取本地文件中缺少的剩余字节。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM