zoukankan      html  css  js  c++  java
  • net core api 文件下载,断点续传

    1、新建 net core api 项目

    2、新建自定义的文件流类

    public class ResFileStream : FileStream
        {
            public ResFileStream(string path, FileMode mode, FileAccess access) : base(path, mode, access)
            {
            }
    
            /// <param name="array"></param>
            /// <param name="offset">偏移量</param>
            /// <param name="count">读取的最大字节数</param>
            /// <returns></returns>
            public override int Read(byte[] array, int offset, int count)
            {
                // 此处可以限制下载速度
                //count = 256;
                //Thread.Sleep(10);
                return base.Read(array, offset, count);
            }
        }

    3、添加 文件下载 api

    /// <summary>
    /// 文件下载 断点续传 视频加载
    /// </summary>
    /// <returns></returns>
    [HttpGet]
    public IActionResult GetFile()
    {
       var filePath = @"H:电影宝贝计划.mp4"; //文件所在路径
       var name = @"宝贝计划.mp4"; //文件名
       ResFileStream fs = new ResFileStream(filePath, FileMode.Open, FileAccess.Read);
    
       var type = new MediaTypeHeaderValue("video/mp4").MediaType;
       return File(fs, contentType: type, name, enableRangeProcessing: true); //enableRangeProcessing 是否启动断点续传
    }
  • 相关阅读:
    Python编程四大神兽:迭代器、生成器、闭包和装饰器
    Linux基础
    3.8记录
    3.7记录
    3.6进度记录
    3.5进度
    3.4进度
    3.3进度
    3.2进度记录
    3.1记录
  • 原文地址:https://www.cnblogs.com/juanheqiao/p/14204790.html
Copyright © 2011-2022 走看看