编程语言
首页 > 编程语言> > c# – .NET Core MVC中的部分内容(用于视频/音频流)

c# – .NET Core MVC中的部分内容(用于视频/音频流)

作者:互联网

我正在尝试在我的网站上实现视频和音频流(以便在Chrome中搜索),我最近发现.NET Core 2.0显然提供了一种使用FileStreamResult实现此功能的相对简单和推荐的方法.

这是我返回FileStreamResult的Action的简化实现:

    public IActionResult GetFileDirect(string f)
    {
        var path = Path.Combine(Defaults.StorageLocation, f);
        return File(System.IO.File.OpenRead(path), "video/mp4");
    } 

File方法具有以下(缩写)描述:

Returns a file in the specified fileStream (Status200OK), with the specified contentType as the Content-Type. This supports range requests (Status206PartialContent or Status416RangeNotSatisfiable if the range is not satisfiable)

但由于某种原因,服务器仍然无法正确响应范围请求.

我错过了什么吗?

更新

Chrome发送的请求如下所示

GET https://myserver.com/viewer/GetFileDirect?f=myvideo.mp4 HTTP/1.1
Host: myserver.com
Connection: keep-alive
Accept-Encoding: identity;q=1, *;q=0
User-Agent: ...
Accept: */*
Accept-Language: ...
Cookie: ...
Range: bytes=0-

响应如下:

HTTP/1.1 200 OK
Server: nginx/1.10.3 (Ubuntu)
Date: Fri, 09 Feb 2018 17:57:45 GMT
Content-Type: video/mp4
Content-Length: 5418689
Connection: keep-alive

[... content ... ]

还尝试使用以下命令:
curl -H范围:bytes = 16- -I https://myserver.com/viewer/GetFileDirect?f=myvideo.mp4并返回相同的响应.

HTML也很简单.

<video controls autoplay>
    <source src="https://myserver.com/viewer/GetFileDirect?f=myvideo.mp4" type="video/mp4">
    Your browser does not support the video tag.
</video>

视频开始播放 – 用户无法搜索视频.

解决方法:

将在版本2.1中的File方法中添加enableRangeProcessing参数.目前,您需要设置一个开关.你可以这两种方式之一:

在runtimeconfig.json中:

{
  // Set the switch here to affect .NET Core apps
  "configProperties": {
    "Switch.Microsoft.AspNetCore.Mvc.EnableRangeProcessing": "true"
  }
}

要么:

 //Enable 206 Partial Content responses to enable Video Seeking from 
 //api/videos/{id}/file,
 //as per, https://github.com/aspnet/Mvc/pull/6895#issuecomment-356477675.
 //Should be able to remove this switch and use the enableRangeProcessing 
 //overload of File once 
 // ASP.NET Core 2.1 released

   AppContext.SetSwitch("Switch.Microsoft.AspNetCore.Mvc.EnableRangeProcessing", 
   true);

有关详情,请参见ASP.NET Core GitHub Repo.

标签:c,asp-net-mvc,streaming,asp-net-core,asp-net-core-webapi
来源: https://codeday.me/bug/20190627/1304988.html