核心代码
public static class DownloadHelper
 {
     /// <summary>
     /// 断点下载
     /// </summary>
     /// <param name="controller"></param>
     /// <param name="fullpath"></param>
     /// <returns></returns>
     public static async Task<long> RangeDownload(this Controller controller, string fullpath,string filename)
     {
         long size, start, end, length, fp = 0;
         using (StreamReader reader = new StreamReader(File.OpenRead(fullpath)))
         {

             size = reader.BaseStream.Length;
             start = 0;
             end = size - 1;
             length = size;
          
             controller.Response.Headers.Add("Accept-Ranges", "0-" + size);

             if (!String.IsNullOrEmpty(controller.Request.Headers["Range"]))
             {
                 long anotherStart = start;
                 long anotherEnd = end;
                 string[] arr_split = controller.Request.Headers["Range"].FirstOrDefault().Split(new char[] { Convert.ToChar("=") });
                 string range = arr_split[1];

                 if (range.IndexOf(",") > -1)
                 {
                     controller.Response.Headers.Add("Content-Range", "bytes " + start + "-" + end + "/" + size);
                     controller.Response.StatusCode = 416;
                 }

                 if (range.StartsWith("-"))
                 {
                     anotherStart = size - Convert.ToInt64(range.Substring(1));
                 }
                 else
                 {
                     arr_split = range.Split(new char[] { Convert.ToChar("-") });
                     anotherStart = Convert.ToInt64(arr_split[0]);
                     long temp = 0;
                     anotherEnd = (arr_split.Length > 1 && Int64.TryParse(arr_split[1].ToString(), out temp)) ? Convert.ToInt64(arr_split[1]) : size;
                 }

                 anotherEnd = (anotherEnd > end) ? end : anotherEnd;
                 if (anotherStart > anotherEnd || anotherStart > size - 1 || anotherEnd >= size)
                 {

                     controller.Response.Headers.Add("Content-Range", "bytes " + start + "-" + end + "/" + size);
                     controller.Response.StatusCode = 416;
                     await controller.Response.WriteAsync("Requested Range Not Satisfiable");
                 }
                 start = anotherStart;
                 end = anotherEnd;

                 length = end - start + 1;
                 fp = reader.BaseStream.Seek(start, SeekOrigin.Begin);
                 controller.Response.StatusCode = 206;
             }
         }
         // Notify the client the byte range we‘ll be outputting
         controller.Response.Headers.Add("Content-Range", "bytes " + start + "-" + end + "/" + size);
         controller.Response.Headers.Add("Content-Length", length.ToString());
         controller.Response.Headers.Add("Content-Disposition", $"attachment; filename={filename}") ;
         // Start buffered download
         await controller.Response.SendFileAsync(fullpath, fp, length);
         return fp;
     }
 }

技术的发展日新月异,随着时间推移,无法保证本博客所有内容的正确性。如有误导,请大家见谅,欢迎评论区指正!

开源库地址,欢迎点亮:

GitHub: https://github.com/ITMingliang

Gitee:  https://gitee.com/mingliang_it

GitLab:  https://gitlab.com/ITMingliang

建群声明: 本着技术在于分享,方便大家交流学习的初心,特此建立【编程内功修炼交流群】,为大家答疑解惑。热烈欢迎各位爱交流学习的程序员进群,也希望进群的大佬能不吝分享自己遇到的技术问题和学习心得!进群方式:扫码关注公众号,后台回复【进群

断点下载帮助方法_下载