C#多线程文件下载器推荐和示例,支持断点续传和限速

2023-07-12  乐帮网

.net

一个开源的C#写的下载封装类,支持.Net Core的快速、跨平台和可靠的多部分下载器
Downloader是一个现代的、流畅的、异步的、可测试的和可移植的.NET库。这是一个具有异步进度事件的多部分下载器。该库可以添加到您的.Net Core v2及更高版本或.Net Framework v4.5及更高版本的项目中。
Downloader兼容.NET Standard 2.0及以上版本,可在Windows、Linux和macOS上运行,支持完整的.NET Framework或.NET Core。

具有以下特别:
简单的下载请求接口。
下载文件同步且无阻塞。
下载任何类型的文件,如图片、视频、pdf、apk等。
跨平台库,可下载任何大小的文件。
获取每个块的实时进度信息。
并行下载多部分文件。
不间断地处理所有客户端和服务器端的异常。
配置您的ChunkCount来定义下载文件的部分数量。
以内存或磁盘模式下载多部分文件。
大块并行保存在最终文件中,而不是临时文件中。
文件大小在下载开始前预先分配。
存储下载包对象,以便在需要时恢复下载。
在每个进度事件中获取下载速度或进度百分比。
获取每个下载块的下载进度事件。
异步快速暂停和恢复下载。
通过包对象随时停止和恢复下载。
支持大文件下载
设置动态下载速度限制(可随时更改速度限制)。
无需存储在磁盘上即可下载文件,并为每个下载文件获取内存流。
可序列化的下载包(从JSON或二进制到JSON或二进制)
支持实时流媒体,适合在下载的同时播放音乐。
能够下载大文件的某一字节范围。
代码小巧、快速,不依赖外部库。
控制下载器在下载过程中消耗的系统内存(RAM)。

使用代码示例:

var downloadOpt = new DownloadConfiguration()
{
    // usually, hosts support max to 8000 bytes, default values is 8000
    BufferBlockSize = 10240,
    // file parts to download, 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 file as parallel or not. 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, default value is 512
    MinimumSizeOfChunking = 1024, 
    // Before starting the download, reserve the storage space of the file as file size, 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
        }
    }
};

var downloader = new DownloadService(downloadOpt);

// 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;

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

还有一些更高级的方法可查看以下地址:
开源地址:https://github.com/bezzad/Downloader

公众号二维码

关注我的微信公众号
在公众号里留言交流
投稿邮箱:1052839972@qq.com

庭院深深深几许?杨柳堆烟,帘幕无重数。
玉勒雕鞍游冶处,楼高不见章台路。
雨横风狂三月暮。门掩黄昏,无计留春住。
泪眼问花花不语,乱红飞过秋千去。

欧阳修

付款二维码

如果感觉对您有帮助
欢迎向作者提供捐赠
这将是创作的最大动力