asp.net-core-2.0 - 在asp.net core 2中流式传输视频文件

标签 asp.net-core-2.0 filestream memorystream

我想使用 asp.net core 在浏览器中播放视频

在html中我有

<video width="320" height="240" controls>
 <source src="http://localhost:55193/api/VideoPlayer/Download" type="video/mp4">
 Your browser does not support the video tag.
</video>

并在asp.net core 2中
    [HttpGet]
    [Route("Download")]
    public async Task<IActionResult> Download()
    {
        var path = @"d:\test\somemovie.mp4";
        var memory = new MemoryStream();
        using (var stream = new FileStream(@"d:\test\somemovie.mp4", FileMode.Open, FileAccess.Read, FileShare.ReadWrite, 65536, FileOptions.Asynchronous | FileOptions.SequentialScan))
        {
            await stream.CopyToAsync(memory);
        }
        memory.Position = 0;
        return File(memory, "application/octet-stream", Path.GetFileName(path));
    }

此代码是否通过流播放文件(我的意思是逐块缓冲文件并播放)?

如果我想从用户设置播放器进度的任何位置播放文件,我该怎么做?

最佳答案

就用普通的return PhysicalFile这里:

public class HomeController : Controller
    {
        public IActionResult Download()
        {
            return PhysicalFile(@"d:\test\somemovie.mp4", "application/octet-stream");
        }

因为它支持流式传输和恢复文件下载所需的范围 header :
enter image description here

还有 return File , FileStreamResultVirtualFileResult支持 partial range requests也。偶static files middleware也支持。

关于asp.net-core-2.0 - 在asp.net core 2中流式传输视频文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51338193/

相关文章:

c# - ASP.NET Core 中的多种身份验证方案

c# - 使用 C# 将 FileStream 编码为 base64

azure - 如何在 Windows 应用商店应用程序中将数据上传到 azure blob 时显示进度条

c# - 与 .Net 邮件附件一起使用时处理 MemoryStream

c# - DeflateStream 复制到 MemoryStream

c# - “DbContextOptionsBuilder”不包含 UseNpgsql() 的定义

c# - 返回带有查询字符串的 View

c# - 带有方括号的模型绑定(bind) POST 值

c++ - 在matlab中移动文件

sql-server - 是否可以访问 FILESTREAM 共享?