.net + html5上传,通过.NET HTML5流式传输MP4视频
发布日期:2021-06-24 12:04:29 浏览次数:2 分类:技术文章

本文共 3650 字,大约阅读时间需要 12 分钟。

固定!

这里的问题可能与范围请求的HTML5视频实现有关.如果不在用于发送视频的页面中支持范围请求,它们将无法工作.

我发现的代码Chris的vb.net端口在下面,为了实现它,只需将它放在你的页面中:

system.web.httpcontext.current.Response.ContentType = "video/" & convertFormat.toString

RangeDownload(convertedVideo, system.web.httpcontext.current)

VB.NET代码:

private sub RangeDownload( fullpath as string, context as HttpContext)

dim size as long

dim start as long

dim theend as long

dim length as long

dim fp as long =0

using reader as new StreamReader(fullpath)

size = reader.BaseStream.Length

start = 0

theend = size - 1

length = size

'/ Now that we've gotten so far without errors we send the accept range header

'* At the moment we only support single ranges.

'* Multiple ranges requires some more work to ensure it works correctly

'* and comply with the spesifications: http://www.w3.org/Protocols/rfc2616/rfc2616-sec19.html#sec19.2

'*

'* Multirange support annouces itself with:

'* header('Accept-Ranges: bytes');

'*

'* Multirange content must be sent with multipart/byteranges mediatype,

'* (mediatype = mimetype)

'* as well as a boundry header to indicate the various chunks of data.

'*/

context.Response.AddHeader("Accept-Ranges", "0-" + size)

'// header('Accept-Ranges: bytes')

'// multipart/byteranges

'// http://www.w3.org/Protocols/rfc2616/rfc2616-sec19.html#sec19.2

if (not String.IsNullOrEmpty(context.Request.ServerVariables("HTTP_RANGE"))) then

dim anotherStart as long = start

dim anotherEnd as long = theend

dim arr_split as string() = context.Request.ServerVariables("HTTP_RANGE").Split("=") 'new char[] { Convert.ToChar("=") })

dim range as string = arr_split(1)

'// Make sure the client hasn't sent us a multibyte range

if (range.IndexOf(",") > -1) then

'// (?) Shoud this be issued here, or should the first

'// range be used? Or should the header be ignored and

'// we output the whole content?

context.Response.AddHeader("Content-Range", "bytes " & start & "-" & theend & "/" & size)

throw new HttpException(416, "Requested Range Not Satisfiable")

end if

'// If the range starts with an '-' we start from the beginning

'// If not, we forward the file pointer

'// And make sure to get the end byte if spesified

if (range.StartsWith("-")) then

'// The n-number of the last bytes is requested

anotherStart = size - Convert.ToInt64(range.Substring(1))

else

arr_split = range.Split("-")

anotherStart = Convert.ToInt64(arr_split(0))

dim temp as long = 0

if (arr_split.Length > 1 andalso Int64.TryParse(arr_split(1).ToString(), temp)) then

anotherEnd = Convert.ToInt64(arr_split(1))

else

anotherEnd = size

end if

end if

'/* Check the range and make sure it's treated according to the specs.

' * http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html

' */

'// End bytes can not be larger than $end.

if (anotherEnd > theend) then

anotherEnd = theend

else

anotherEnd = anotherEnd

end if

'// Validate the requested range and return an error if it's not correct.

if (anotherStart > anotherEnd or anotherStart > size - 1 or anotherEnd >= size) then

context.Response.AddHeader("Content-Range", "bytes " + start + "-" + theend + "/" + size)

throw new HttpException(416, "Requested Range Not Satisfiable")

end if

start = anotherStart

theend = anotherEnd

length = theend - start + 1 '// Calculate new content length

fp = reader.BaseStream.Seek(start, SeekOrigin.Begin)

context.Response.StatusCode = 206

end if

end using

'// Notify the client the byte range we'll be outputting

context.Response.AddHeader("Content-Range", "bytes " & start & "-" & theend & "/" & size)

context.Response.AddHeader("Content-Length", length.ToString())

'// Start buffered download

context.Response.WriteFile(fullpath, fp, length)

context.Response.End()

end sub

转载地址:https://blog.csdn.net/weixin_32637935/article/details/117854476 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:华三s5000配置镜像接口_H3C S5000交换机简单配置
下一篇:android webview签名,android – WebView.setWebContentsDebuggingEnabled(false)但我可以在安装签名APK后调试代码...

发表评论

最新留言

路过按个爪印,很不错,赞一个!
[***.219.124.196]2024年04月28日 18时15分39秒