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

问题描述 投票:0回答:3

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


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

FileStreamResult

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

方法具有以下(简短的)描述:


返回指定fileStream中的文件(Status200OK),指定contentType为Content-Type。这支持范围请求(如果范围不可满足,则 Status206PartialContent 或 Status416RangeNotSatisfiable)

但是由于某种原因,服务器仍然没有正确响应范围请求。

我错过了什么吗?

更新

从 Chrome 发送的请求如下所示

File

响应看起来像:

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 ... ]

并且它返回相同的响应。


HTML 也非常简单。

curl -H Range:bytes=16- -I https://myserver.com/viewer/GetFileDirect?f=myvideo.mp4

视频确实开始播放 - 用户无法搜索视频。

c# asp.net-mvc asp.net-core streaming asp.net-core-webapi
3个回答
26
投票

<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>

这允许在浏览器中查找。


11
投票

在runtimeconfig.json中:

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

或:

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

有关详细信息,请参阅 
ASP.NET Core GitHub 存储库


0
投票
Paulo Neves

的回答足够好,我提供了更多信息,让 bytes[] fileContent 可以这样做: //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);

© www.soinside.com 2019 - 2024. All rights reserved.