zoukankan      html  css  js  c++  java
  • 解决 Asp.Net5 在视频文件下载预览时无法快进的问题

    前情提要

    https://www.cnblogs.com/puzhiwei/p/15265005.html

    在解决.Net5 如何修改Content-Disposition实现在线预览的功能后,我又遇到了新的问题,那就是在预览视频文件时无法拖动进度条。我尝试了多种解决方案都没有解决这个问题,首先我先对这个问题进行了一番搜索,但是并没有发现什么解决方法。但是在Google时,我发现这个问题是一个分段下载的问题。于是我就搜索了.Net5分段下载。

    果然,我找到了一些.Net5分段下载的文章。

    像这个 https://www.cnblogs.com/xxred/p/7931757.html

    以及这个 https://www.cnblogs.com/tianma3798/p/13445111.html

    但是都不太好用。

    于是我就开始了继续搜索。

    wwwroot下文件的加载

    我尝试将视频文件放到.Net默认的静态文件加载目录wwwroot下,然后直接访问wwwroot里的视频文件,我发现这个是可以拖动的,并且响应头与请求头中多出了Content-Range这个信息。

    MDN 上说 在HTTP协议中,响应首部 Content-Range 显示的是一个数据片段在整个文件中的位置。

    那么问题就很清楚了,就是如何实现在视频传输时添加Content-Range的问题,于是我又开始了搜索 .net5 file Content-Range,别说,还真找到了一些方案

    这个

    https://www.codeproject.com/Articles/820146/HTTP-Partial-Content-In-ASP-NET-Web-API-Video

    还有这个

    https://www.c-sharpcorner.com/UploadFile/vendettamit/handling-range-specific-content-request-in-webapi-Asp-Net/

    过于复杂且不知道好不好用,然后我就接着搜。

    Github 的结果

    于是我就在Github上找到了这个https://github.com/aspnet/Mvc/pull/6895

    看了看好像就是说 return File() 时如何实现分段传输文件的方法的。

    看了看使用方法好像还是不太会用,不过不重要,我已经知道该搜索什么了。

    enableRangeProcessing

    接着搜 enableRangeProcessing 我就发现了这篇文章 https://www.cnblogs.com/tianma3798/p/13445147.html ,其中提到了enableRangeProcessing 直接设置 true 即可启用对按范围返回文件流的支持,至此问题解决。

    最新代码

            [Route("load")]
            [HttpGet]
            public async Task<IActionResult> DownloadFile(string path, string type)
            {
                if (string.IsNullOrEmpty(path))
                {
                    return Content("404 for not found!");
                }
    
                try
                {
                    // 获取文件的ContentType
                    string fileExt = Path.GetExtension(path);
                    var provider = new FileExtensionContentTypeProvider();
                    var memi = provider.Mappings[fileExt];
                    if (type == "inline")
                    {
                        Response.Headers.Add("Content-Disposition", $"inline; filename={System.Net.WebUtility.UrlEncode(Path.GetFileName(filePath))}");
                        return PhysicalFile(filePath, memi, true);
                        //return File(memoryStream, memi, true);
                    }
                    return PhysicalFile(filePath, memi, Path.GetFileName(filePath), true);
                }
                catch (DirectoryNotFoundException e)
                {
                    _logger.LogError($"文件:{path},没有找到!
    {e.Message}");
                    return Content("404 for not found!");
                }
            }
    

    总结

    通过这次问题的解决,告诉我一个道理,要是一个问题一开始搜索没有什么解决方案,说不定看一下源码才是问题最好的解决方案。就能看到enableRangeProcessing这个参数,快速的去解决这个问题。

           //
            // 摘要:
            //     Returns the file specified by physicalPath (Microsoft.AspNetCore.Http.StatusCodes.Status200OK)
            //     with the specified contentType as the Content-Type. This supports range requests
            //     (Microsoft.AspNetCore.Http.StatusCodes.Status206PartialContent or Microsoft.AspNetCore.Http.StatusCodes.Status416RangeNotSatisfiable
            //     if the range is not satisfiable).
            //
            // 参数:
            //   physicalPath:
            //     The path to the file. The path must be an absolute path.
            //
            //   contentType:
            //     The Content-Type of the file.
            //
            //   enableRangeProcessing:
            //     Set to true to enable range requests processing.
            //
            // 返回结果:
            //     The created Microsoft.AspNetCore.Mvc.PhysicalFileResult for the response.
    

    版权信息

    本文首发于 https://www.buguagaoshu.com/archives/aspnet5传输视频文件无法快进

  • 相关阅读:
    运行时动态的创建form的方法
    用X++代码来动态的改变表的属性
    使用WinAPI类来查找文件
    用循环得到表中所有的字段
    用X++建立和调用报表(Report)
    JAVA 保留字
    Cygwin使用
    系统程序员成长计划-算法与容器(三) (上)
    系统程序员成长计划工程管理(二)
    系统程序员成长计划-算法与容器(三) (下)
  • 原文地址:https://www.cnblogs.com/puzhiwei/p/15361074.html
Copyright © 2011-2022 走看看