Hey Codeproject,
I've been following a couple of articles on how to create a pause/resume functionality on my downloads, and basically creating a downloader from scratch.
What seems to happen is that it requests data and writes this to the specified file. I'm also able to skip a given amount of bytes.
The following is my download code:
private void Worker()
{
const int chunkSize = 1024;
var webRequest = (HttpWebRequest) WebRequest.Create(Download.Query);
webRequest.UseDefaultCredentials = true;
webRequest.Method = "GET";
webRequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)";
long progress = 0;
using (var fileStream = new FileStream(Path, FileMode.CreateNew))
{
using (var myHttpWebResponse = (HttpWebResponse)webRequest.GetResponse())
{
using (var receiveStream = myHttpWebResponse.GetResponseStream())
{
var fileSize = myHttpWebResponse.ContentLength;
var read = new byte[chunkSize];
int count;
while ((count = receiveStream.Read(read, 0, chunkSize)) > 0 && Downloading)
{
fileStream.Write(read, 0, count);
progress += count;
OnDownloadProgressChanged?.Invoke(this, new ProgressChangedDownloadEvent(fileSize, progress));
if (Stopping)
break;
}
}
}
}
if(!Stopping)
OnDownloadFinished?.Invoke(this, new DownloadFinishedEvent(Path));
}
Am I right to assume that I could just skip the amount of bytes if the given file already exists on the computer in order to "continue" downloading when for instance my application restarts?
In other words, can I just determine the size of the existing file, and add that as a "range" to the web request?
I'd like to note that this is not about pausing/resuming a download, rather the ability to pick up where it left off in case the software shuts down unexpectedly. So you could call it a "retry" but without beginning all the way back from the start.
What I have tried:
I've provided code as for the current progress.