static void Main(string[] args) { var mtd = new MultiThreadDownloader("https://git.imweb.io/ldqk0/imgbed/raw/master/2020/01/05/sdcgssa1wlxc.jpg", Environment.GetEnvironmentVariable("temp"), "Y:\\1.jpg", 8); mtd.Configure(req => { req.Referer = "https://masuit.com"; req.Headers.Add("Origin", "https://baidu.com"); }); mtd.TotalProgressChanged += (sender, e) => { var downloader = sender as MultiThreadDownloader; Console.WriteLine("下载进度:" + downloader.TotalProgress + "%"); Console.WriteLine("下载速度:" + downloader.TotalSpeedInBytes / 1024 / 1024 + "MBps"); }; mtd.FileMergeProgressChanged += (sender, e) => { Console.WriteLine("下载完成"); }; mtd.Start();//开始下载 Console.ReadKey(); }
/// <summary> /// 下载文件 /// </summary> /// <param name="downloading"></param> /// <param name="urls"></param> /// <param name="path"></param> /// <param name="localFileName"></param> /// <returns></returns> private bool DownloadByBuiltin(DownloadingItem downloading, List <string> urls, string path, string localFileName) { // path已斜杠结尾,去掉斜杠 path = path.TrimEnd('/').TrimEnd('\\'); foreach (var url in urls) { // 创建下载器 var mtd = new MultiThreadDownloader(url, Environment.GetEnvironmentVariable("temp"), Path.Combine(path, localFileName), SettingsManager.GetInstance().GetSplit()); // 配置网络请求 mtd.Configure(req => { req.CookieContainer = LoginHelper.GetLoginInfoCookies(); req.UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/97.0.4692.99 Safari/537.36"; req.Referer = "https://www.bilibili.com"; req.Headers.Add("Origin", "https://www.bilibili.com"); if (SettingsManager.GetInstance().IsHttpProxy() == AllowStatus.YES) { req.Proxy = new WebProxy(SettingsManager.GetInstance().GetHttpProxy(), SettingsManager.GetInstance().GetHttpProxyListenPort()); } }); // 下载进度回调 mtd.TotalProgressChanged += (sender, e) => { try { // 状态更新 var downloader = sender as MultiThreadDownloader; // 下载进度百分比 float percent = downloader.TotalProgress; // 根据进度判断本次是否需要更新UI if (Math.Abs(percent - downloading.Progress) < 0.01) { return; } if (Math.Abs(percent - downloading.Progress) > 5) { return; } // 下载进度 downloading.Progress = percent; // 下载大小 downloading.DownloadingFileSize = Format.FormatFileSize(downloader.TotalBytesReceived) + "/" + Format.FormatFileSize(downloader.Size); // 下载速度 long speed = (long)downloader.TotalSpeedInBytes; // 下载速度显示 downloading.SpeedDisplay = Format.FormatSpeed(speed); // 最大下载速度 if (downloading.Downloading.MaxSpeed < speed) { downloading.Downloading.MaxSpeed = speed; } } catch (InvalidOperationException ex) { Core.Utils.Debugging.Console.PrintLine($"{Tag}.DownloadByBuiltin()发生InvalidOperationException异常: {0}", ex); LogManager.Error($"{Tag}.DownloadByBuiltin()", ex); } catch (Exception ex) { Core.Utils.Debugging.Console.PrintLine($"{Tag}.DownloadByBuiltin()发生异常: {0}", ex); LogManager.Error($"{Tag}.DownloadByBuiltin()", ex); } }; // 文件合并完成回调 bool isComplete = false; mtd.FileMergedComplete += (sender, e) => { // 跳出循环 if (File.Exists(Path.Combine(path, localFileName))) { isComplete = true; } }; // 开始下载 mtd.StartAsync(); // 阻塞当前任务,监听暂停事件 while (!isComplete) { cancellationToken.ThrowIfCancellationRequested(); switch (downloading.Downloading.DownloadStatus) { case DownloadStatus.PAUSE: // 暂停下载 mtd.Pause(); // 通知UI,并阻塞当前线程 Pause(downloading); break; case DownloadStatus.DOWNLOADING: break; } Thread.Sleep(100); } return(isComplete); } return(false); }