bezzad / Downloader

Fast, cross-platform and reliable multipart downloader with asynchronous progress events for .NET applications.
MIT License
1.25k stars 193 forks source link

Values of DownloadProgressChangedEventArgs after resume of download #121

Closed NeekoDev closed 1 year ago

NeekoDev commented 1 year ago

Hi,

I'im trying to resume download of large file after exit/restart program. But values of DownloadProgressChangedEventArgs seems incorrect.

I think I forgot a step? I followed it though : https://github.com/bezzad/Downloader/blob/master/src/Downloader.Test/IntegrationTests/DownloadIntegrationTest.cs#L122

  1. Starting the download of file (Archive of 1GB)
  2. Percentage, bytes received etc are OK
  3. Downloading 300MB, exit the program
  4. Restart the program and continue downloading
  5. Percentage = 100 (why?) - and total / received bytes go back to 0
public async Task<bool> DownloadArchive()
{
    var downloadOpt = new DownloadConfiguration()
    {

    };

    var downloader = new DownloadService(downloadOpt);

    downloader.DownloadProgressChanged += OnDownloadProgressChanged;

    await downloader.DownloadFileTaskAsync(url, IPath);

    return true;
}

private void OnDownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
{

}

Do I miss something?

Thanks ! :)

bezzad commented 1 year ago

Hi @NeekoGta , You must do the following steps to cancel and resume a file.

var downloadOpt = new DownloadConfiguration();
var downloader = new DownloadService(downloadOpt);

// download your file
await downloader.DownloadFileTaskAsync(url, file);

// At first, keep and store the Package file to resume 
// your download from the last download position:
DownloadPackage pack = downloader.Package;

// then call this function to break your stream and cancel progress.
downloader.CancelAsync();

// store your package file in a file or database
// resume download with package 
await downloader.DownloadFileTaskAsync(pack);

If these steps are OK, but you have the issue again. Please attach your "download config" and "download URL" here. I'll check your URL and tell you what's the problem.

Note: Some servers can't resume downloading because they don't accept downloads in a range. So, the Downloer also can't request to resume downloading from special bytes.