隐藏

C# .NET 6.0 Http异步Async多线程文件下载,M3U8下载,显示下载进度 使用Nuget包 CocoaAni.Download

发布:2023/4/6 12:26:56作者:管理员 来源:本站 浏览次数:634

项目地址


Gitee:CocoaAni.Download

HTTP下载任务参数配置


   /// <summary>

   /// 下载任务名称

   /// </summary>

   public string TaskName = DownloadConfig.DefaultTaskName;

   /// <summary>

   /// 下载URL地址

   /// </summary>

   public string Url = DownloadConfig.UndefinedUrl;

   /// <summary>

   /// 保存路径

   /// </summary>

   public string SavePath = DownloadConfig.SaveOnMemory;

   /// <summary>

   /// 使用的任务数量

   /// </summary>

   public int TaskCount = DownloadConfig.DefaultTaskCount;

   /// <summary>

   /// 下载的开始范围Byte

   /// </summary>

   public long RangeStart = 0;

   /// <summary>

   /// 下载的结束范围Byte

   /// </summary>

   public long RangeEnd = 0;

   /// <summary>

   /// 错误重试次数

   /// </summary>

   public int ErrorRetries = DownloadConfig.DefaultErrorRetries;

   /// <summary>

   /// 子任务错误重试次数

   /// </summary>

   public int SubTaskErrorRetries = DownloadConfig.DefaultSubTaskErrorRetries;

   /// <summary>

   /// 进度更新触发事件阈值

   /// </summary>

   public float ProgressNotifyThreshold = DownloadConfig.DefaultProgressNotifyThreshold;

   /// <summary>

   /// 子任务通知主任务进度更新 子任务需要达到的进度

   /// </summary>

   public float SubTaskProgressNotifyParentThreshold = DownloadConfig.DefaultSubTaskProgressNotifyParentThreshold;

   /// <summary>

   /// 添加的Http请求头

   /// </summary>

   public Dictionary<string, string> RequestHeaders = DownloadConfig.DefaultHttpRequestHeaders;

   /// <summary>

   /// 下载使用的驱动

   /// </summary>

   public IHttpFileDownloadDriver? Driver;

   /// <summary>

   /// 下载成功事件

   /// </summary>

   public event EventHandler<DownloadStateChangeEventArgs>? DownloadSuccess;

   /// <summary>

   /// 下载进度更新事件

   /// </summary>

   public event EventHandler<DownloadStateChangeEventArgs>? ProgressUpdate;

   /// <summary>

   /// 下载发生错误事件

   /// </summary>

   public event EventHandler<DownloadStateChangeEventArgs>? DownloadError;

   /// <summary>

   /// 下载开始事件

   /// </summary>

   public event EventHandler<DownloadStateChangeEventArgs>? DownloadStart;

   /// <summary>

   /// 下载结束事件

   /// </summary>


  


M3U8下载任务参数配置


参数继承自Http下载参数,以下是独有参数


/// <summary>

   /// 是否合并TS

   /// </summary>

   public bool IsMergeTs = DownloadConfig.DefaultM3U8IsMergeTs;

   /// <summary>

   /// 是否保存TS片段

   /// </summary>

   public bool IsSaveSubTs = DownloadConfig.DefaultM3U8IsSaveSubTs;


 


HTTP文件下载案例


using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

using CocoaAni.Download;

using CocoaAni.Download.Args;

using CocoaAni.Download.Tasks;


namespace CocoaAniDownloadDemo;


public class DownloadHttpFileSampleDemo

{

   public static void Main(string[] args)

   {

       var task = new HttpDownloadTask(new HttpDownloadArgs()

       {

           TaskName = "HttpFileTest",

           SavePath = "D:/idea.exe",

           TaskCount = 1,

           Url = "https://download.jetbrains.com.cn/idea/ideaIU-2022.2.1.exe?_gl=1*1gg3ou9*_ga*NzU3NDA5MjI0LjE2NjE0MDA5NzM.*_ga_9J976DJZ68*MTY2MTQxODAxNi4zLjAuMTY2MTQxODAxNi4wLjAuMA..&_ga=2.239127811.104737429.1661400973-757409224.1661400973"

       });

       task.Args.DownloadSuccess += (sender, eventArgs) =>

       {

           Console.WriteLine($"下载成功了!文件大小是{eventArgs.DownloadResult.FileSize/DownloadConfig.MB} MB");

       };

       task.Args.ProgressUpdate += (sender, eventArgs) =>

       {

           Console.WriteLine($"下载进度:[{eventArgs.DownloadResult.Progress*100:00.00}%]");

       };

       task.Args.DownloadError += (sender, eventArgs) =>

       {

           Console.WriteLine($"下载失败! {eventArgs.DownloadResult.Error!.Message}");

       };

       task.ExecuteAsync().Wait();

   }

}


 


M3U8文件下载案例


using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

using CocoaAni.Download;

using CocoaAni.Download.Args;

using CocoaAni.Download.Tasks;


namespace CocoaAniDownloadDemo;


public class DownloadM3U8FileSampleDemo

{

   public static void Main(string[] args)

   {

       var task = new M3U8DownloadTask(new M3U8DownloadArgs()

       {

           TaskName = "M3u8Test",

           SavePath = "D:/M3U8.ts",

           TaskCount = 1,

           IsSaveSubTs = true,//保存TS片段

           Url = "http://xxxx.xxxx.xxxx.m3u8"

       });

       task.Args.DownloadSuccess += (sender, eventArgs) =>

       {

           Console.WriteLine($"下载成功了!文件大小是{eventArgs.DownloadResult.Stream!.Length / DownloadConfig.MB} MB");

       };

       task.Args.ProgressUpdate += (sender, eventArgs) =>

       {

           Console.WriteLine($"下载进度:[{eventArgs.DownloadResult.Progress * 100:00.00}%]");

       };

       task.Args.DownloadError += (sender, eventArgs) =>

       {

           Console.WriteLine($"下载失败! {eventArgs.DownloadResult.Error!.Message}");

       };

       task.ExecuteAsync().Wait();

   }