Downloader 2.3.7

There is a newer version of this package available.
See the version list below for details.
dotnet add package Downloader --version 2.3.7                
NuGet\Install-Package Downloader -Version 2.3.7                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Downloader" Version="2.3.7" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Downloader --version 2.3.7                
#r "nuget: Downloader, 2.3.7"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install Downloader as a Cake Addin
#addin nuget:?package=Downloader&version=2.3.7

// Install Downloader as a Cake Tool
#tool nuget:?package=Downloader&version=2.3.7                

Windows x64 Ubuntu x64 Build Status codecov NuGet NuGet CodeFactor Codacy Badge License Generic badge FOSSA Status

Downloader

🚀 Fast and reliable multipart downloader with .Net Core 3.1+ supporting 🚀

Downloader is a modern, fluent, asynchronous, testable and portable library for .NET. This is a multipart downloader with asynchronous progress events. This library can added in your .Net Core v3.1 and later or .Net Framework v4.5 or later projects.

Downloader is compatible with .NET Standard 2.0 and above, running on Windows, Linux, and macOS, in full .NET Framework or .NET Core.

Sample Console Application

sample-project

Features at a glance

  • Simple interface to make download request.
  • Download files async and non-blocking.
  • Download any type of files like image, video, pdf, apk and etc.
  • Cross-platform library to download any files with any size.
  • Get real-time progress info of each block.
  • Download file multipart as parallel.
  • Handle all the client-side and server-side exceptions non-stopping.
  • Config your ChunkCount to define the parts count of the download file.
  • Download file multipart as in-memory or in-temp files cache mode.
  • Store download package object to resume the download when you want.
  • Get download speed or progress percentage in each progress event.
  • Get download progress events per chunk downloads.
  • Pause and Resume your downloads with package object.
  • Supports large file download.
  • Set a dynamic speed limit on downloads (changeable speed limitation on the go).
  • Download files without storing on disk and get a memory stream for each downloaded file.
  • Serializable download package (to/from JSON or Binary)
  • Live streaming support, suitable for playing music at the same time as downloading.
  • Ability to download just a certain range of bytes of a large file.

Installing via NuGet

PM> Install-Package Downloader

Installing via the .NET Core command line interface

dotnet add package Downloader

How to use

Step 1: Create your custom configuration

Simple Configuration

var downloadOpt = new DownloadConfiguration()
{
    ChunkCount = 8, // file parts to download, default value is 1
    OnTheFlyDownload = true, // caching in-memory or not? default values is true
    ParallelDownload = true // download parts of file as parallel or not. Default value is false
};

Complex Configuration

var downloadOpt = new DownloadConfiguration()
{
    BufferBlockSize = 10240,    // usually, hosts support max to 8000 bytes, default values is 8000
    ChunkCount = 8,             // file parts to download, default value is 1
    MaximumBytesPerSecond = 1024 * 1024 * 2, // download speed limited to 2MB/s, default values is zero or unlimited
    MaxTryAgainOnFailover = 5,  // the maximum number of times to fail
    OnTheFlyDownload = false,   // caching in-memory or not? default values is true
    ParallelDownload = true,    // download parts of file as parallel or not. Default value is false
    ParallelCount = 4,          // number of parallel downloads. The default value is the same as the chunk count
    TempDirectory = @"C:\temp", // Set the temp path for buffering chunk files, the default path is Path.GetTempPath()
    Timeout = 1000,             // timeout (millisecond) per stream block reader, default values is 1000
    RangeDownload = false,      // set true if you want to download just a specific range of bytes of a large file
    RangeLow = 0,               // floor offset of download range of a large file
    RangeHigh = 0,              // ceiling offset of download range of a large file
    RequestConfiguration = 
    {
        // config and customize request headers
        Accept = "*/*",
        CookieContainer = cookies,
        Headers = new WebHeaderCollection(),     // { Add your custom headers }
        KeepAlive = true,                        // default value is false
        ProtocolVersion = HttpVersion.Version11, // default value is HTTP 1.1
        UseDefaultCredentials = false,
        UserAgent = $"DownloaderSample/{Assembly.GetExecutingAssembly().GetName().Version?.ToString(3)}"
        // Proxy = new WebProxy() {
        //    Address = new Uri("http://YourProxyServer/proxy.pac"),
        //    UseDefaultCredentials = false,
        //    Credentials = System.Net.CredentialCache.DefaultNetworkCredentials,
        //    BypassProxyOnLocal = true
        // }
    }
};

Step 2: Create download service instance per download and pass your config

var downloader = new DownloadService(downloadOpt);

Step 3: Handle download events

// Provide `FileName` and `TotalBytesToReceive` at the start of each downloads
downloader.DownloadStarted += OnDownloadStarted;

// Provide any information about chunker downloads, like progress percentage per chunk, speed, total received bytes and received bytes array to live streaming.
downloader.ChunkDownloadProgressChanged += OnChunkDownloadProgressChanged;

// Provide any information about download progress, like progress percentage of sum of chunks, total speed, average speed, total received bytes and received bytes array to live streaming.
downloader.DownloadProgressChanged += OnDownloadProgressChanged;

// Download completed event that can include occurred errors or cancelled or download completed successfully.
downloader.DownloadFileCompleted += OnDownloadFileCompleted;

Step 4: Start the download with the url and file name

string file = @"Your_Path\fileName.zip";
string url = @"https://file-examples.com/fileName.zip";
await downloader.DownloadFileTaskAsync(url, file);

Step 4b: Start the download without file name

DirectoryInfo path = new DirectoryInfo("Your_Path");
string url = @"https://file-examples.com/fileName.zip";
await downloader.DownloadFileTaskAsync(url, path); // download into "Your_Path\fileName.zip"

Step 4c: Download in MemoryStream

Stream destinationStream = await downloader.DownloadFileTaskAsync(url);

How to stop and resume downloads

The ‍DownloadService class has a property called Package that stores each step of the download. To stopping or pause the download you must call the CancelAsync method, and if you want to continue again, you must call the same DownloadFileTaskAsync function with the Package parameter to resume your download! For example:

Keep Package file to resume from last download positions:

DownloadPackage pack = downloader.Package;

Stop or Pause Download:

downloader.CancelAsync();

Resume Download:

await downloader.DownloadFileTaskAsync(pack);

So that you can even save your large downloads with a very small amount in the Package and after restarting the program, restore it again and start continuing your download. In fact, the packages are your instant download snapshots. If your download config has OnTheFlyDownload, the downloaded bytes ​​will be stored in the package itself, but otherwise, only the downloaded file address will be included and you can resume it whenever you like. For more detail see StopResumeDownloadTest method

Note: for complete sample see Downloader.Sample project from this repository.


Fluent download builder usage

For easy and fluent use of the downloader, you can use the DownloadBuilder class. Consider the following examples:

Simple usage:

await DownloadBuilder.New()
    .WithUrl(@"https://host.com/test-file.zip")
    .WithDirectory(@"C:\temp")
    .Build()
    .StartAsync();

Complex usage:

IDownload download = DownloadBuilder.New()
    .WithUrl(@"https://host.com/test-file.zip")
    .WithDirectory(@"C:\temp")
    .WithFileName("test-file.zip")
    .WithConfiguration(new DownloadConfiguration())
    .Build();

download.DownloadProgressChanged += DownloadProgressChanged;
download.DownloadFileCompleted += DownloadFileCompleted;
download.DownloadStarted += DownloadStarted;
download.ChunkDownloadProgressChanged += ChunkDownloadProgressChanged;

await download.StartAsync();

Resume the existing download package:

await DownloadBuilder.Build(package).StartAsync();

Resume the existing download package with a new configuration:

await DownloadBuilder.Build(package, new DownloadConfiguration()).StartAsync();

When does Downloader fail to download in multiple chunks?

Content-Length:

If your URL server does not provide the file size in the response header (Content-Length). The Downloader cannot split the file into multiple parts and continues its work with one chunk.

Accept-Ranges:

If the server return Accept-Ranges: none in the responses header then that means the server does not support download in range and the Downloader cannot use multiple chunking and continues its work with one chunk.

Content-Range:

At first, the Downloader sends a GET request to the server to fetch the file's size in the range. If the server does not provide Content-Range in the header then that means the server does not support download in range. Therefore, the Downloader has to continue its work with one chunk.


How to serialize and deserialize downloader package

What is Serialization?

Serialization is the process of converting an object's state into information that can be stored for later retrieval or that can be sent to another system. For example, you may have an object that represents a document that you wish to save. This object could be serialized to a stream of binary information and stored as a file on disk. Later the binary data can be retrieved from the file and deserialized into objects that are exact copies of the original information. As a second example, you may have an object containing the details of a transaction that you wish to send to a non-.NET system. This information could be serialised to XML before being transmitted. The receiving system would convert the XML into a format that it could understand.

In this section, we want to show how to serialize download packages to JSON text or Binary, after stopping download to keep download data and resuming that every time you want. You can serialize packages even using memory storage for caching download data which is used MemoryStream.

Binary Serialization

To serialize or deserialize the package into a binary file, just you need to a BinaryFormatter of IFormatter and then create a stream to write bytes on that:

DownloadPackage pack = downloader.Package;
IFormatter formatter = new BinaryFormatter();
Stream serializedStream = new MemoryStream();

Serializing package:

formatter.Serialize(serializedStream, pack);

Deserializing into the new package:

var newPack = formatter.Deserialize(serializedStream) as DownloadPackage;

For more detail see PackageSerializationTest method.

JSON Serialization

Serializing the package to JSON is very simple like this:

var packageJson = JsonConvert.SerializeObject(package);

But to deserializing the IStorage Storage property of chunks you need to declare a JsonConverter to override the Read method of JsonConverter. So you should add the below converter to your application:

public class StorageConverter : Newtonsoft.Json.JsonConverter<IStorage>
{
    public override void WriteJson(JsonWriter writer, IStorage value, JsonSerializer serializer)
    {
        throw new NotImplementedException();
    }

    public override IStorage ReadJson(JsonReader reader, Type objectType, IStorage existingValue, bool hasExistingValue, JsonSerializer serializer)
    {
        if (reader.TokenType == JsonToken.Null)
            return null;

        var obj = JObject.Load(reader); // Throws an exception if the current token is not an object.
        if (obj.ContainsKey(nameof(FileStorage.FileName)))
        {
            var filename = obj[nameof(FileStorage.FileName)]?.Value<string>();
            return new FileStorage(filename);
        }

        if (obj.ContainsKey(nameof(MemoryStorage.Data)))
        {
            var data = obj[nameof(MemoryStorage.Data)]?.Value<string>();
            return new MemoryStorage() { Data = data };
        }

        return null;
    }
}

Then you can deserialize your packages from JSON:

var settings = new Newtonsoft.Json.JsonSerializerSettings();
settings.Converters.Add(new StorageConverter());
var newPack = Newtonsoft.Json.JsonConvert.DeserializeObject<DownloadPackage>(serializedJson, settings);

For more detail see PackageSerializationTest method

Instructions for Contributing

Welcome to contribute, feel free to change and open a PullRequest to develop branch. You can use either the latest version of Visual Studio or Visual Studio Code and .NET CLI for Windows, Mac and Linux.

For GitHub workflow, check out our Git workflow below this paragraph. We are following the excellent GitHub Flow process, and would like to make sure you have all of the information needed to be a world-class contributor!

Git Workflow

The general process for working with Downloader is:

  1. Fork on GitHub
  2. Make sure your line endings are correctly configured and fix your line endings!
  3. Clone your fork locally
  4. Configure the upstream repo (git remote add upstream git://github.com/bezzad/downloader)
  5. Switch to the latest development branch (eg vX.Y.Z, using git checkout vX.Y.Z)
  6. Create a local branch from that (git checkout -b myBranch).
  7. Work on your feature
  8. Rebase if required
  9. Push the branch up to GitHub (git push origin myBranch)
  10. Send a Pull Request on GitHub - the PR should target (have as base branch) the latest development branch (eg vX.Y.Z) rather than master.

We accept pull requests from the community. But, you should never work on a clone of master, and you should never send a pull request from master - always from a branch. Please be sure to branch from the head of the latest vX.Y.Z develop branch (rather than master) when developing contributions.

License

Licensed under the terms of the MIT License

FOSSA Status

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 is compatible. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 is compatible. 
.NET Framework net452 is compatible.  net46 was computed.  net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • .NETCoreApp 3.1

    • No dependencies.
  • .NETFramework 4.5.2

    • No dependencies.
  • .NETStandard 2.0

    • No dependencies.
  • .NETStandard 2.1

    • No dependencies.
  • net6.0

    • No dependencies.

NuGet packages (10)

Showing the top 5 NuGet packages that depend on Downloader:

Package Downloads
Wabbajack.Networking.Http

Package Description

SquareMinecraftLauncher.Core

BaiBaoStudio

EasyUpdate

Easy Update 提供简单的自动更新服务。

Orobouros

A fully-featured and modular online scraper tool. Yes we know the name is spelled wrong. Icon Credit: Hyliian @ DeviantArt

Dove.Avalonia.Extensions.WebView

WebView Extensions for Avalonia.

GitHub repositories (21)

Showing the top 5 popular GitHub repositories that depend on Downloader:

Repository Stars
2dust/v2rayN
A GUI client for Windows and Linux, support Xray core and sing-box-core and others
rocksdanister/lively
Free and open-source software that allows users to set animated desktop wallpapers and screensavers powered by WinUI 3.
goatcorp/FFXIVQuickLauncher
Custom launcher for FFXIV
yaobiao131/downkyicore
哔哩下载姬(跨平台版)downkyi,哔哩哔哩网站视频下载工具,支持批量下载,支持8K、HDR、杜比视界,提供工具箱(音视频提取、去水印等)。
Paving-Base/APK-Installer
An Android Application Installer for Windows
Version Downloads Last updated
3.3.1 2,811 11/28/2024
3.3.0 1,220 11/20/2024
3.2.1 4,778 10/4/2024
3.2.0 1,460 9/22/2024
3.1.2 21,367 6/30/2024
3.1.0-beta 743 1/2/2024
3.0.6 85,940 6/6/2023
3.0.5 397 6/3/2023
3.0.4 48,329 3/11/2023
3.0.3 4,589 1/29/2023
3.0.2 1,157 1/7/2023
3.0.1 5,287 11/2/2022
3.0.0-beta 195 10/12/2022
2.4.1 12,152 9/21/2022
2.4.0 1,073 9/16/2022
2.3.9 521 9/14/2022
2.3.8 888 9/5/2022
2.3.7 1,741 8/23/2022
2.3.6 645 8/20/2022
2.3.5 79,360 5/6/2022
2.3.4 1,219 5/3/2022
2.3.3 5,454 2/23/2022
2.3.2 1,485 1/24/2022
2.3.1 643 1/2/2022
2.3.0 5,133 11/15/2021
2.2.9 10,470 8/12/2021
2.2.8 36,332 4/1/2021
2.2.7 717 3/31/2021
2.2.6 3,787 3/26/2021
2.2.5 1,032 3/24/2021
2.2.4 686 3/19/2021
2.2.3 2,874 3/1/2021
2.2.2 805 2/24/2021
2.2.1 397 2/23/2021
2.2.0 426 2/22/2021
2.1.4 411 2/21/2021
2.1.3 379 2/19/2021
2.1.2 555 2/14/2021
2.1.1 413 2/14/2021
2.1.0 440 2/10/2021
2.0.9 467 2/4/2021
2.0.8 518 2/3/2021
2.0.7 609 1/24/2021
2.0.6 447 1/13/2021
2.0.5 495 1/10/2021
2.0.4 649 1/5/2021
2.0.3 447 1/2/2021
2.0.1 605 12/19/2020
2.0.0 637 12/6/2020
1.9.9 707 12/1/2020
1.9.8 447 12/1/2020
1.9.7 545 11/12/2020
1.9.6 488 11/11/2020
1.9.5 563 11/11/2020 1.9.5 is deprecated because it is no longer maintained and has critical bugs.
1.9.4 567 10/24/2020
1.9.3 489 10/19/2020
1.9.2 469 10/12/2020
1.9.1 525 9/28/2020
1.9.0 558 9/27/2020
1.8.0 858 7/31/2020
1.7.0 772 7/17/2020
1.6.0 540 7/14/2020
1.5.0 533 7/6/2020
1.4.0 614 7/4/2020
1.3.0 964 6/21/2020
1.2.1 607 6/21/2020
1.2.0 583 6/16/2020
1.1.0 567 5/29/2020
1.0.9 591 5/16/2020
1.0.8 536 5/11/2020
1.0.7 577 5/3/2020
1.0.6 533 4/22/2020
1.0.5 526 4/21/2020
1.0.4 560 4/16/2020
1.0.3 662 3/28/2020
1.0.2 556 3/28/2020
1.0.1 1,131 3/28/2020

Fixed parallel downloading when a server not support download in rang #98 #99