Downloader 3.1.2
See the version list below for details.
dotnet add package Downloader --version 3.1.2
NuGet\Install-Package Downloader -Version 3.1.2
<PackageReference Include="Downloader" Version="3.1.2" />
paket add Downloader --version 3.1.2
#r "nuget: Downloader, 3.1.2"
// Install Downloader as a Cake Addin #addin nuget:?package=Downloader&version=3.1.2 // Install Downloader as a Cake Tool #tool nuget:?package=Downloader&version=3.1.2
Downloader
🚀 Fast, cross-platform and reliable multipart downloader with .Net Core 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 be added to your .Net Core v2
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.
For a complete example see Downloader.Sample project from this repository.
Sample Console Application
Features at a glance
- Simple interface to make download requests.
- Download files async and non-blocking.
- Download any file like image, video, pdf, apk, etc.
- Cross-platform library to download any files of any size.
- Get real-time progress info on 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
oron-disk
mode. - Chunks are saved in parallel on the final file, not the temp files.
- The file size is pre-allocated before the download starts.
- Store the 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.
- Fast Pause and Resume download asynchronously.
- Stop and Resume downloads whenever you want with the package object.
- Supports large file download.
- Set a dynamic speed limit on downloads (changeable speed limitation on the go).
- Download files without storing them on disk and get a memory stream for each downloaded file.
- Serializable download package (to/from
JSON
orBinary
) - 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.
- Code is tiny, fast and does not depend on external libraries.
- Control the amount of system memory (RAM) the Downloader consumes during downloading.
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, the default value is 1
ParallelDownload = true // download parts of the file as parallel or not. The default value is false
};
Complex Configuration
Note: Do not use all of the below options in your applications, just add which one you need.
var downloadOpt = new DownloadConfiguration()
{
// usually, hosts support max to 8000 bytes, default value is 8000
BufferBlockSize = 10240,
// file parts to download, the default value is 1
ChunkCount = 8,
// download speed limited to 2MB/s, default values is zero or unlimited
MaximumBytesPerSecond = 1024*1024*2,
// the maximum number of times to fail
MaxTryAgainOnFailover = 5,
// release memory buffer after each 50 MB
MaximumMemoryBufferBytes = 1024 * 1024 * 50,
// download parts of the file as parallel or not. The default value is false
ParallelDownload = true,
// number of parallel downloads. The default value is the same as the chunk count
ParallelCount = 4,
// timeout (millisecond) per stream block reader, default values is 1000
Timeout = 1000,
// set true if you want to download just a specific range of bytes of a large file
RangeDownload = false,
// floor offset of download range of a large file
RangeLow = 0,
// ceiling offset of download range of a large file
RangeHigh = 0,
// clear package chunks data when download completed with failure, default value is false
ClearPackageOnCompletionWithFailure = true,
// minimum size of chunking to download a file in multiple parts, the default value is 512
MinimumSizeOfChunking = 1024,
// Before starting the download, reserve the storage space of the file as file size, the default value is false
ReserveStorageSpaceBeforeStartingDownload = true;
// config and customize request headers
RequestConfiguration =
{
Accept = "*/*",
CookieContainer = cookies,
Headers = new WebHeaderCollection(), // { your custom headers }
KeepAlive = true, // default value is false
ProtocolVersion = HttpVersion.Version11, // default value is HTTP 1.1
UseDefaultCredentials = false,
// your custom user agent or your_app_name/app_version.
UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64)",
Proxy = new WebProxy() {
Address = new Uri("http://YourProxyServer/proxy.pac"),
UseDefaultCredentials = false,
Credentials = System.Net.CredentialCache.DefaultNetworkCredentials,
BypassProxyOnLocal = true
}
}
};
Step 2: Create a 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 download
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 errors or
// canceled 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";
// download into "Your_Path\fileName.zip"
await downloader.DownloadFileTaskAsync(url, path);
Step 4c: Download in MemoryStream
// After download completion, it gets a MemoryStream
Stream destinationStream = await downloader.DownloadFileTaskAsync(url);
How to pause and resume downloads quickly
When you want to resume a download quickly after pausing a few seconds. You can call the Pause
function of the downloader service. This way, streams stay alive and are only suspended by a locker to be released and resumed whenever you want.
// Pause the download
DownloadService.Pause();
// Resume the download
DownloadService.Resume();
How to stop and resume downloads other time
The DownloadService
class has a property called Package
that stores each step of the download. To stop the download you must call the CancelAsync
method. Now, if you want to continue again, you must call the same DownloadFileTaskAsync
function with the Package
parameter to resume your download. For example:
// At first, keep and store the Package file to resume
// your download from the last download position:
DownloadPackage pack = downloader.Package;
Stop or cancel download:
// This function breaks your stream and cancels progress.
downloader.CancelAsync();
Resuming download after cancelation:
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 and start continuing your download. The packages are your snapshot of the download instance. Only the downloaded file addresses will be included in the package and you can resume it whenever you want. For more detail see StopResumeDownloadTest method
Note: Sometimes a server does not support downloading in a specific range. That time, we can't resume downloads after canceling. So, the downloader starts from the beginning.
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();
download.Stop(); // cancel current download
Resume the existing download package:
await DownloadBuilder.Build(package).StartAsync();
Resume the existing download package with a new configuration:
await DownloadBuilder.Build(package, config).StartAsync();
var download = DownloadBuilder.New()
.Build()
.WithUrl(url)
.WithFileLocation(path);
await download.StartAsync();
download.Pause(); // pause current download quickly
download.Resume(); // continue current download quickly
When does the 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 returns 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 the 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 another type of system. This information could be serialized 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 the download to keep downloading data and resuming that every time you want.
You can serialize packages even using memory storage for caching download data which is used MemoryStream
.
JSON Serialization
Serializing the package to JSON
is very simple like this:
var packageJson = JsonConvert.SerializeObject(package);
Deserializing into the new package:
var newPack = JsonConvert.DeserializeObject<DownloadPackage>(packageJson);
For more detail see PackageSerializationTest method
Binary Serialization
To serialize or deserialize the package into a binary file, first, you need to serialize it to JSON and next save it with BinaryWriter.
NOTE: The BinaryFormatter type is dangerous and is not recommended for data processing. Applications should stop using BinaryFormatter as soon as possible, even if they believe the data they're processing to be trustworthy. BinaryFormatter is insecure and can't be made secure. So, BinaryFormatter is deprecated and we can no longer support it. Reference
Instructions for Contributing
Welcome to contribute, feel free to change and open a PullRequest to develop the 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:
- Fork on GitHub
- Make sure your line endings are correctly configured and fix your line endings!
- Clone your fork locally
- Configure the upstream repo (
git remote add upstream git://github.com/bezzad/downloader
) - Switch to the latest development branch (eg vX.Y.Z, using
git checkout vX.Y.Z
) - Create a local branch from that (
git checkout -b myBranch
). - Work on your feature
- Rebase if required
- Push the branch up to GitHub (
git push origin myBranch
) - Send a Pull Request on GitHub - the PR should target (have as a base branch) the latest development branch (eg
vX.Y.Z
) rather thanmaster
.
We accept pull requests from the community. But, you should never work on a clone of the master, and you should never send a pull request from the 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.
You can run tests with the Docker Compose file with the following command:
docker-compose -p downloader up
Or with docker file:
docker build -f ./dockerfile -t downloader-linux .
docker run --name downloader-linux-container -d downloader-linux --env=ASPNETCORE_ENVIRONMENT=Development .
Or run the following command to call docker directly:
docker run --rm -v ${pwd}:/app --env=ASPNETCORE_ENVIRONMENT=Development -w /app/tests mcr.microsoft.com/dotnet/sdk:6.0 dotnet test ../ --logger:trx
License
Licensed under the terms of the MIT License
Contributors
Thanks go to these wonderful people (List made with contrib.rocks):
<a href="https://github.com/bezzad/downloader/graphs/contributors"> <img src="https://contrib.rocks/image?repo=bezzad/downloader" /> </a>
Product | Versions 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 is compatible. 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 | net461 was computed. net462 is compatible. 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. |
-
.NETCoreApp 3.1
- No dependencies.
-
.NETFramework 4.6.2
- No dependencies.
-
.NETStandard 2.0
- No dependencies.
-
.NETStandard 2.1
- No dependencies.
-
net6.0
- No dependencies.
-
net8.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 (19)
Showing the top 5 popular GitHub repositories that depend on Downloader:
Repository | Stars |
---|---|
2dust/v2rayN
A GUI client for Windows, support Xray core and v2fly 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.2.1 | 2,422 | 10/4/2024 | |
3.2.0 | 1,317 | 9/22/2024 | |
3.1.2 | 17,464 | 6/30/2024 | |
3.1.0-beta | 727 | 1/2/2024 | |
3.0.6 | 77,792 | 6/6/2023 | |
3.0.5 | 387 | 6/3/2023 | |
3.0.4 | 41,543 | 3/11/2023 | |
3.0.3 | 4,386 | 1/29/2023 | |
3.0.2 | 1,107 | 1/7/2023 | |
3.0.1 | 5,259 | 11/2/2022 | |
3.0.0-beta | 186 | 10/12/2022 | |
2.4.1 | 12,097 | 9/21/2022 | |
2.4.0 | 1,036 | 9/16/2022 | |
2.3.9 | 511 | 9/14/2022 | |
2.3.8 | 879 | 9/5/2022 | |
2.3.7 | 1,712 | 8/23/2022 | |
2.3.6 | 638 | 8/20/2022 | |
2.3.5 | 79,248 | 5/6/2022 | |
2.3.4 | 1,208 | 5/3/2022 | |
2.3.3 | 5,434 | 2/23/2022 | |
2.3.2 | 1,464 | 1/24/2022 | |
2.3.1 | 634 | 1/2/2022 | |
2.3.0 | 5,091 | 11/15/2021 | |
2.2.9 | 10,321 | 8/12/2021 | |
2.2.8 | 34,369 | 4/1/2021 | |
2.2.7 | 708 | 3/31/2021 | |
2.2.6 | 3,778 | 3/26/2021 | |
2.2.5 | 1,021 | 3/24/2021 | |
2.2.4 | 677 | 3/19/2021 | |
2.2.3 | 2,867 | 3/1/2021 | |
2.2.2 | 795 | 2/24/2021 | |
2.2.1 | 387 | 2/23/2021 | |
2.2.0 | 415 | 2/22/2021 | |
2.1.4 | 403 | 2/21/2021 | |
2.1.3 | 370 | 2/19/2021 | |
2.1.2 | 546 | 2/14/2021 | |
2.1.1 | 405 | 2/14/2021 | |
2.1.0 | 431 | 2/10/2021 | |
2.0.9 | 460 | 2/4/2021 | |
2.0.8 | 509 | 2/3/2021 | |
2.0.7 | 601 | 1/24/2021 | |
2.0.6 | 439 | 1/13/2021 | |
2.0.5 | 486 | 1/10/2021 | |
2.0.4 | 641 | 1/5/2021 | |
2.0.3 | 437 | 1/2/2021 | |
2.0.1 | 595 | 12/19/2020 | |
2.0.0 | 623 | 12/6/2020 | |
1.9.9 | 699 | 12/1/2020 | |
1.9.8 | 439 | 12/1/2020 | |
1.9.7 | 537 | 11/12/2020 | |
1.9.6 | 480 | 11/11/2020 | |
1.9.5 | 554 | 11/11/2020 | |
1.9.4 | 557 | 10/24/2020 | |
1.9.3 | 481 | 10/19/2020 | |
1.9.2 | 459 | 10/12/2020 | |
1.9.1 | 517 | 9/28/2020 | |
1.9.0 | 548 | 9/27/2020 | |
1.8.0 | 848 | 7/31/2020 | |
1.7.0 | 764 | 7/17/2020 | |
1.6.0 | 531 | 7/14/2020 | |
1.5.0 | 525 | 7/6/2020 | |
1.4.0 | 606 | 7/4/2020 | |
1.3.0 | 955 | 6/21/2020 | |
1.2.1 | 598 | 6/21/2020 | |
1.2.0 | 574 | 6/16/2020 | |
1.1.0 | 559 | 5/29/2020 | |
1.0.9 | 581 | 5/16/2020 | |
1.0.8 | 524 | 5/11/2020 | |
1.0.7 | 568 | 5/3/2020 | |
1.0.6 | 524 | 4/22/2020 | |
1.0.5 | 517 | 4/21/2020 | |
1.0.4 | 551 | 4/16/2020 | |
1.0.3 | 653 | 3/28/2020 | |
1.0.2 | 550 | 3/28/2020 | |
1.0.1 | 1,123 | 3/28/2020 |
Download the same file from multiple sources #118
Control the amount of system memory (RAM) that the Downloader consumes during downloading.
Refactor all codes and test.
User xUnit instead of msunit tests.
Improve memory performance.
Fixed some bugs.