在C#WPF中继续下载 [英] Resuming download in C# WPF

查看:66
本文介绍了在C#WPF中继续下载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,


我正在开发一个WPF应用程序,它下载一些大于100 MB的MSI文件。下载时,如果互联网断开连接,则必须从下载中断的位置恢复当前下载的文件。我使用WebClient
和Cookies来下载文件。如果互联网断开并再次连接,则不会恢复文件。



使用(CookieAwareWebClient客户端=新CookieAwareWebClient())

{

        client.DownloadProgressChanged + = WebClientDownloadProgressChanged;

    client.DownloadFileCompleted + = new AsyncCompletedEventHandler(Client_DownloadFileCompleted);      

  client.DownloadFileAsync(url,fileName);

 }



static void WebClientDownloadProgressChanged(对象发件人, 

DownloadProgressChangedEventArgs e)

    {

        Console.WriteLine("下载状态:{0}%。",e.ProgressPercentage);      



    }


    static void Client_DownloadFileCompleted(object sender,AsyncCompletedEventArgs e)

    {

        Console.WriteLine("下载完成!");

    }       
$


}



公共类CookieAwareWebClient:WebClient

{

    private readonly CookieContainer m_container = new CookieContainer();



   受保护的覆盖WebRequest GetWebRequest(Uri地址)

    {

        WebRequest request = base.GetWebRequest(address);

        HttpWebRequest webRequest =请求为HttpWebRequest;

        if(webRequest!= null)

        {

            webRequest.CookieContainer = m_container;

        }¥b $ b       退货要求;

    }
}



我还使用webRequest.AddRange尝试了以下代码。它对我不起作用。考虑必须下载10 MB文件。如果下载了5 MB文件并且再次下载,则调用GetWebrequest方法会将文件大小初始化为零,并且
webRequest.AddRange会下载剩余文件。即AddRange下载剩余的5 MB文件。因此,只有5MB文件将被下载,因为在调用GetWebrequest方法时,首次下载的5 MB被初始化为零。



公共类WebclientEx:WebClient

    {

       受保护的覆盖WebRequest GetWebRequest(Uri地址)

        {

            HttpWebRequest webRequest = base.GetWebRequest(address)as HttpWebRequest;

            if(downloadedsize> 0L)

                webRequest.AddRange((int)downloadedsize);

            return(WebRequest)webRequest;

        }


    } b
$ b有人可以提供恢复文件下载的建议吗?


谢谢,


Manivannan S. 


解决方案

Hi Manivannan2618,


感谢您在此发帖。


根据您的描述,您想恢复文件下载。


您可以参考以下链接。


https://codereview.stackexchange .com / questions / 97872 / resumable-http-download-class


注意:此响应包含对第三方万维网站点的引用。 Microsoft提供此信息是为了方便您。


Microsoft不控制这些站点,也未测试在这些站点上找到的任何软件或信息;因此,Microsoft不能对在那里找到的任何软件或信息的质量,安全性或适用性做出任何陈述。


使用互联网上的任何软件都存在固有的危险, Microsoft提醒您在从Internet检索任何软件之前确保完全了解风险。 


希望我的建议有所帮助。


最好的问候,


杰克






Hi Everyone,

I’m developing a WPF application which downloads some MSI files which are greater than 100 MB. While downloading, if the internet is disconnected, the currently downloading file has to resume from where the download was interrupted. I used the WebClient and Cookies for downloading the files. The files are not resumed if the internet was disconnected and connected again.

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

    }

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

I have also tried the below code using webRequest.AddRange. It doesn't work for me. Consider 10 MB file has to be downloaded. If 5 MB of file was downloaded and while downloading again, calling the GetWebrequest method initializes the file size to zero and webRequest.AddRange downloads the remaining file. i.e AddRange downloads the remaining 5 MB file. So, only 5MB file will be downloaded as the first downloaded 5 MB was initialized to zero in calling GetWebrequest method.

public class WebclientEx:WebClient
    {
        protected override WebRequest GetWebRequest(Uri address)
        {
            HttpWebRequest webRequest = base.GetWebRequest(address) as HttpWebRequest;
            if (downloadedsize> 0L)
                webRequest.AddRange((int)downloadedsize);
            return (WebRequest)webRequest;
        }

    }

Can anyone provide suggestion for resuming the file download?

Thanks,

Manivannan S. 

解决方案

Hi Manivannan2618,

Thank you for posting here.

According to your description, you want to resume the file download.

You could refer to the below link.

https://codereview.stackexchange.com/questions/97872/resumable-http-download-class

Note: This response contains a reference to a third-party World Wide Web site. Microsoft is providing this information as a convenience to you.

Microsoft does not control these sites and has not tested any software or information found on these sites; Therefore, Microsoft cannot make any representations regarding the quality, safety, or suitability of any software or information found there.

There are inherent dangers in the use of any software found on the Internet, and Microsoft cautions you to make sure that you completely understand the risk before retrieving any software from the Internet. 

Hope my advice could be helpful.

Best Regards,

Jack



这篇关于在C#WPF中继续下载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆