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 是否启动断点续传 }