aspnetcore 实现断点续传

扩展方法,改编自http://blogs.visigo.com/chriscoulson/easy-handling-of-http-range-requests-in-asp-net/

/// <summary>
        /// 断点下载
        /// </summary>
        /// <param name="controller"></param>
        /// <param name="fullpath"></param>
        /// <returns></returns>
        public static async Task<long> RangeDownload(this Controller controller, string fullpath)
        {
            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;
                // Now that we've gotten so far without errors we send the accept range header
                /* At the moment we only support single ranges.
                 * Multiple ranges requires some more work to ensure it works correctly
                 * and comply with the spesifications: http://www.w3.org/Protocols/rfc2616/rfc2616-sec19.html#sec19.2
                 *
                 * Multirange support annouces itself with:
                 * header('Accept-Ranges: bytes');
                 *
                 * Multirange content must be sent with multipart/byteranges mediatype,
                 * (mediatype = mimetype)
                 * as well as a boundry header to indicate the various chunks of data.
                 */
                controller.Response.Headers.Add("Accept-Ranges", "0-" + size);
                // header('Accept-Ranges: bytes');
                // multipart/byteranges
                // http://www.w3.org/Protocols/rfc2616/rfc2616-sec19.html#sec19.2

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

                    // Make sure the client hasn't sent us a multibyte range
                    if (range.IndexOf(",") > -1)
                    {
                        // (?) Shoud this be issued here, or should the first
                        // range be used? Or should the header be ignored and
                        // we output the whole content?
                        controller.Response.Headers.Add("Content-Range", "bytes " + start + "-" + end + "/" + size);
                        controller.Response.StatusCode = 416;
                        controller.Response.StatusCode = 416;
                        await controller.Response.WriteAsync("Requested Range Not Satisfiable");
                    }

                    // If the range starts with an '-' we start from the beginning
                    // If not, we forward the file pointer
                    // And make sure to get the end byte if spesified
                    if (range.StartsWith("-"))
                    {
                        // The n-number of the last bytes is requested
                        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;
                    }
                    /* Check the range and make sure it's treated according to the specs.
                     * http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html
                     */
                    // End bytes can not be larger than $end.
                    anotherEnd = (anotherEnd > end) ? end : anotherEnd;
                    // Validate the requested range and return an error if it's not correct.
                    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; // Calculate new content length
                    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());
            // Start buffered download
            await controller.Response.SendFileAsync(fullpath, fp, length);
            return fp;
        }

 

控制器方法内调用样例:

/// <summary>
        /// 下载指定版本
        /// </summary>
        /// <param name="id"></param>
        /// <param name="version"></param>
        /// <returns></returns>
        public async Task Download(Guid id, int version)
        {
            string path = null;
            var state = appService.GetAppState(id, version);
            if (state == Model.AppState.Available)
            {
                path = appService.GetZipFromAppList(id, version);
                if (!System.IO.File.Exists(path))
                {
                    await this.StatusCode(404).ExecuteResultAsync(this.ControllerContext);
                }
                var offset = await this.RangeDownload(path);
                if (offset == 0)
                {
                    appService.IncreaseNDownload(state, id, version);
                }
            }
            else
            {
                await this.StatusCode(404).ExecuteResultAsync(this.ControllerContext);
            }
}

 

转载于:https://www.cnblogs.com/mrtiny/p/aspnetcore_range_download.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
一个基于ASP.NET Core的可伸缩、通用的文件服务器。 通常后端项目可能会有头像、图片、音频、视频等上传/下载需求,这些需求都可以抽象为文件服务。 功能特点 支持Linux(推荐)、Windows 可伸缩式架构,支持部署1-N台文件服务器 RESTful架构的API接口,支持多语言客户端 支持文件秒传、断点续传、远程拉取上传 支持为用户指定磁盘空间配额 支持自定义文件处理器 系统架构 文件的上传/下载通常由客户端直接与文件服务器交互,上传时需要提供代表用户身份token(由业务服务器生成),成功后会返回文件根地址。 也可以直接由业务服务器上传返回文件根地址给客户端。 源码中包含基于.Net Standard的服务端SDK,可以生成token、上传文件等 源码中包含基于.Net Standard的客户端SDK,可以上传/下载文件等 后端使用 配置业务服务器 //Startup.cs代码片段 public void ConfigureServices(IServiceCollection services) { //.... services.AddFileService(opts => { opts.Host = "fs.mondol.info"; //文件服务器域名 opts.AppSecret = "xxxxxx"; //加密密钥,需要与文件服务器相同 }); } 生成访问令牌 IFileServiceManager fileSvceMgr; //此实例可通过DI框架获得 //根据业务规定其意义,例如:1-代表管理员,2-代表用户 var ownerType = 2; var ownerId = 2; //如果ownerType=2,则为用户ID var validTime = TimeSpan.FromDays(2); //token有效期 var ownerToken = fileSvceMgr.GenerateOwnerTokenString(ownerType, ownerId, validTime); 前端使用 文件上传 IFileServiceClient fileClient; //此实例可通过DI框架获得 var ownerToken = "业务服务器返回的token"; var periodMinute = 0; //有效期,0不过期 var updResult = await fileClient.UploadAsync(ownerToken, "文件路径", periodMinute); var url = updResult.Data.Url; //得到文件根地址 标签:文件服务器

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值