简体   繁体   English

使用 ASP.NET Core 3 流式传输视频

[英]Streaming videos with ASP.NET Core 3

I'm currently building a API in ASP.NET Core 3 as my first project with .NET Core.我目前正在 ASP.NET Core 3 中构建 API,作为我使用 .NET Core 的第一个项目。

I'm currently trying to send a video to my React.js frontend to watch it in the browser.我目前正在尝试将视频发送到我的 React.js 前端以在浏览器中观看。 Uploading files and videos does work without a problem and the method you see down below also already sends a file to the client but if the video is longer than a few seconds, the video player is really slow and it also takes a long time to skip a few seconds of the video.上传文件和视频确实没有问题,您在下面看到的方法也已经将文件发送到客户端,但是如果视频长于几秒钟,则视频播放器确实很慢,并且跳过也需要很长时间几秒钟的视频。 I think that's because the file is first completely downloaded and than played.我认为这是因为文件首先被完全下载然后播放。

[Route("getFileById")]
public FileResult getFileById(int fileId)
{

    var context = new DbContext();

    var file = context.File.Find(fileId);

    if (file == null)
    {
        Console.WriteLine("file " + fileId + " not found");
        return null;
    }

    var content  = new FileStream(file.Name, FileMode.Open, FileAccess.Read, FileShare.Read);
    var response = File(content, "application/octet-stream");
    return response;
}

I think the way to solve my problem is to stream the file and not to send it as a whole.我认为解决我的问题的方法是流式传输文件而不是整体发送。 I've already googled on how to stream videos with ASP.NET Core 3 but I only find websites explaining it for ASP.NET Core 2 (eg http://anthonygiretti.com/2018/01/16/streaming-video-asynchronously-in-asp-net-core-2-with-web-api/ )我已经在谷歌上搜索了如何使用 ASP.NET Core 3 流式传输视频,但我只找到为 ASP.NET Core 2 解释它的网站(例如http://anthonygiretti.com/2018/01/16/streaming-video-asynchronously -in-asp-net-core-2-with-web-api/ )

I've already tried to use the code on these websites but the way they've done it is not compatible to ASP.NET Core 3.我已经尝试使用这些网站上的代码,但他们的做法与 ASP.NET Core 3 不兼容。

How can I stream files in ASP.NET Core 3?如何在 ASP.NET Core 3 中流式传输文件?

If you want to stream the video in the browser, your server should support HTTP range requests .如果您想在浏览器中流式传输视频,您的服务器应支持HTTP 范围请求 In such case, the server is able to send just a small portion of a content requested by the client.在这种情况下,服务器只能发送客户端请求的内容的一小部分。 As you want to stream video in the browser, you can use video html tag that requests for a content using range headers.当您想在浏览器中流式传输视频时,您可以使用video html 标签,该标签使用范围标头请求内容。 Therefore you can also skip some time and immediately play the movie from that position, before it is completely downloaded.因此,您也可以跳过一些时间并在完全下载之前立即从该位置播放电影。

ASP.NET Core 3 already has support for HTTP range requests, it is implemented in PhysicalFile method which has attribute enableRangeProcessing . ASP.NET Core 3 已经支持 HTTP 范围请求,它是在具有属性enableRangeProcessing 的PhysicalFile方法中实现的 As documentation says:正如文档所说:

Returns the file specified by physicalPath (Status200OK), the specified contentType as the Content-Type, and the specified fileDownloadName as the suggested file name.返回由 physicalPath (Status200OK) 指定的文件,指定的 contentType 作为 Content-Type,指定的 fileDownloadName 作为建议的文件名。 This supports range requests (Status206PartialContent or Status416RangeNotSatisfiable if the range is not satisfiable).这支持范围请求(如果范围不可满足,则为 Status206PartialContent 或 Status416RangeNotSatisfiable)。

[Route("getFileById")]
public FileResult getFileById(int fileId)
{
    ...
    return PhysicalFile($"C:/movies/{file.Name}", "application/octet-stream", enableRangeProcessing: true);
}

Note that the path have to be absolute (not relative).请注意,路径必须是绝对的(不是相对的)。

dont forget than pyshicalfile return too partial content look不要忘记比 pyshicalfile 返回太部分内容的外观

link 关联

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM