M
Manivannan2618
Guest
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.
Continue reading...
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.
Continue reading...