Click here to Skip to main content
15,888,235 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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:

C#
/// <summary>
/// The worker function.
/// </summary>
private void Worker()
{
    const int chunkSize = 1024;

    // Create a web request to the download.
    var webRequest = (HttpWebRequest) WebRequest.Create(Download.Query);

    // Use default credentials
    webRequest.UseDefaultCredentials = true;
    webRequest.Method = "GET";
    webRequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)";

    // Bytes or "progress" already read can be added here
    //webRequest.AddRange(range);

    // The current progress
    long progress = 0;

    // Create a new file stream to write to.
    using (var fileStream = new FileStream(Path, FileMode.CreateNew))
    {
        // Get the web response
        using (var myHttpWebResponse = (HttpWebResponse)webRequest.GetResponse())
        {
            // Get the recieve stream.
            using (var receiveStream = myHttpWebResponse.GetResponseStream())
            {
                var fileSize = myHttpWebResponse.ContentLength;

                // Create a new array with the size of our chunks.
                var read = new byte[chunkSize];

                // The amount of bytes we read.
                int count;

                // Read the bytes as long as we are downloading.
                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;
                }
            }


        }
    }

    // At this point the download finished unless we requested to stop.
    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.
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900