html链接mp4格式的文件,HTML5-如何流式传输大型.mp4文件?

Here is the solution I used to create a Web API Controller in C# (MVC) that will serve video files with Byte Ranges (partial requests). Partial requests allow a browser to only download as much of the video as it needs to play rather than downloading the entire video. This makes it far more efficient.

Note this only works in recent versions.

var stream = new FileStream(videoFilename, FileMode.Open, FileAccess.Read , FileShare.Read);

var mediaType = MediaTypeHeaderValue.Parse($"video/{videoFormat}");

if (Request.Headers.Range != null)

{

try

{

var partialResponse = Request.CreateResponse(HttpStatusCode.PartialContent);

partialResponse.Content = new ByteRangeStreamContent(stream, Request.Headers.Range, mediaType);

return partialResponse;

}

catch (InvalidByteRangeException invalidByteRangeException)

{

return Request.CreateErrorResponse(invalidByteRangeException);

}

}

else

{

// If it is not a range request we just send the whole thing as normal

var fullResponse = Request.CreateResponse(HttpStatusCode.OK);

fullResponse.Content = new StreamContent(stream);

fullResponse.Content.Headers.ContentType = mediaType;

return fullResponse;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值