在 C# WPF 中恢复下载

Posted

技术标签:

【中文标题】在 C# WPF 中恢复下载【英文标题】:Resuming download in C# WPF 【发布时间】:2019-05-30 06:59:13 【问题描述】:

我正在开发一个 WPF 应用程序,它可以下载一些大于 100 MB 的 MSI 文件。下载时,如果互联网断开,当前正在下载的文件必须从下载中断的地方恢复。我使用 WebClient 和 Cookies 下载文件。如果 Internet 断开并重新连接,则不会恢复文件。我使用了以下代码。谁能建议我如何实现简历流程?

using (CookieAwareWebClient client = new CookieAwareWebClient())

        client.DownloadProgressChanged += WebClientDownloadProgressChanged;
   client.DownloadFileCompleted += new AsyncCompletedEventHandler(Client_DownloadFileCompleted);      
  client.DownloadFileAsync(url, fileName);
 

static void WebClientDownloadProgressChanged(object sender, 
DownloadProgressChangedEventArgs e)
    
        Console.WriteLine("Download status: 0%.", e.ProgressPercentage);
        Console.WriteLine(e.BytesReceived.ToString());

    

    static void Client_DownloadFileCompleted(object sender, AsyncCompletedEventArgs e)
    
        Console.WriteLine("Download finished!");
           



public class CookieAwareWebClient : WebClient

    private readonly CookieContainer m_container = new CookieContainer();

    protected override WebRequest GetWebRequest(Uri address)
    
        WebRequest request = base.GetWebRequest(address);
        HttpWebRequest webRequest = request as HttpWebRequest;
        if (webRequest != null)
        
            webRequest.CookieContainer = m_container;
        
        return request;
    

【问题讨论】:

【参考方案1】:

要恢复下载,您可以再次建立连接并指定要下载的数据的偏移量。此外,您还可以请求下载部分数据。

  // establishing the connection
  HttpWebRequest oHttpWebRequest = (HttpWebRequest) WebRequest.Create ("myHttpAddress");

  #if partialDownload

  // reading the range 1000-2000 of the data
  oHttpWebRequest.AddRange (1000, 2000);
  // creating the file
  FileInfo oFileInfo = new FileInfo ("myFilename");
  FileStream oFileStream = oFileInfo.Create ();

  #else

  // reading after the last received byte
  FileInfo oFileInfo = new FileInfo ("myFilename");
  oHttpWebRequest.AddRange (oFileInfo.Length);
  // opening the file for appending the data
  FileStream oFileStream = File.Open (oFileInfo.FullName, FileMode.Append);

  #endif

  // opening the connection
  HttpWebResponse oHttpWebResponse = (HttpWebResponse) oHttpWebRequest.GetResponse ();
  Stream oReceiveStream = oHttpWebResponse.GetResponseStream ();

  // reading the html stream and writing into the file
  byte [] abBuffer = new byte [1000000];
  int iReceived = oReceiveStream.Read (abBuffer, 0, abBuffer.Length);
  while ( iReceived > 0 )
   
    oFileStream.Write (abBuffer, 0, iReceived);
    iReceived = oReceiveStream.Read (abBuffer, 0, abBuffer.Length);
  ;
  // closing and disposing the resources
  oFileStream     .Close   ();
  oFileStream     .Dispose ();
  oReceiveStream  .Close   ();
  oReceiveStream  .Dispose ();
  oHttpWebResponse.Close   ();
  oHttpWebResponse.Dispose ();

对于调试,您可以通过查看 Web 请求的返回标头来查看接收到的数据。例如,对于部分下载,您可能会获得

Content-Range: bytes 1000-2000/1156774
Content-Length: 1001

您从

获取标题的键
oHttpWebResponse.Headers.Keys []

以及来自

的值
oHttpWebResponse.Headers []

【讨论】:

以上是关于在 C# WPF 中恢复下载的主要内容,如果未能解决你的问题,请参考以下文章

C#中,当鼠标移动到按钮上时,按钮边框闪烁,鼠标移开时恢复正常,急急急

如何从进程中恢复隐藏的 wpf 窗口

C# SQL 将数据库恢复到默认数据位置

WPF MaterialDesignInXamlToolkit锁屏恢复后页面冻结的问题

C# wpf 实现任意控件拖动

C#窗口切换问题:如何切换回主窗体