使用ASP.NET Core 3流式传输视频 [英] Streaming videos with ASP.NET Core 3

查看:1353
本文介绍了使用ASP.NET Core 3流式传输视频的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在ASP.NET Core 3中构建API,这是我使用.NET Core的第一个项目.

I'm currently building a API in ASP.NET Core 3 as my first project with .NET Core.

我目前正在尝试将视频发送到我的React.js前端,以便在浏览器中观看.上载文件和视频确实可以正常工作,并且您在下面看到的方法也已经将文件发送到客户端,但是如果视频长于几秒钟,则视频播放器确实很慢,并且跳过时间也很长几秒钟的视频.我认为这是因为文件首先被完全下载并播放.

I'm currently trying to send a video to my React.js frontend to watch it in the browser. 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;
}

我认为解决问题的方法是流式传输文件,而不是整体发送文件. 我已经搜索过如何使用ASP.NET Core 3流式传输视频,但是我只找到解释ASP.NET Core 2的网站(例如

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 (e.g. http://anthonygiretti.com/2018/01/16/streaming-video-asynchronously-in-asp-net-core-2-with-web-api/)

我已经尝试在这些网站上使用代码,但是他们这样做的方式与ASP.NET Core 3不兼容.

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?

推荐答案

如果要在浏览器中流式传输视频,则服务器应支持

If you want to stream the video in the browser, your server should support HTTP range requests. 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. Therefore you can also skip some time and immediately play the movie from that position, before it is completely downloaded.

ASP.NET Core 3已经支持HTTP范围请求,它是通过具有属性 enableRangeProcessing PhysicalFile方法实现的.正如文档所述:

ASP.NET Core 3 already has support for HTTP range requests, it is implemented in PhysicalFile method which has attribute enableRangeProcessing. As documentation says:

返回由physicalPath(Status200OK)指定的文件, 指定contentType作为Content-Type,并指定 fileDownloadName作为建议的文件名.这支持范围 请求(Status206PartialContent或Status416RangeNotSatisfiable如果 范围无法满足).

Returns the file specified by physicalPath (Status200OK), the specified contentType as the Content-Type, and the specified fileDownloadName as the suggested file name. This supports range requests (Status206PartialContent or Status416RangeNotSatisfiable if the range is not satisfiable).

[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).

这篇关于使用ASP.NET Core 3流式传输视频的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆