none
Resuming download in C# WPF RRS feed

  • Question

  • 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. 

    Thursday, January 17, 2019 8:51 AM

All replies

  • 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



    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.



    Monday, January 21, 2019 6:24 AM
    Moderator