.net + html5上传,通过.NET HTML5流式传输MP4视频

固定!

这里的问题可能与范围请求的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

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值