这是我的代码(我通过网络搜索获得)
public class VIDeosController : APIController{ // GET API/values public httpResponseMessage Get(string filename) { var filePath = httpContext.Current.Server.MapPath("~") + filename; if (!file.Exists(filePath)) return new httpResponseMessage(httpStatusCode.NotFound); var response = Request.CreateResponse(); response.headers.AcceptRanges.Add("bytes"); var streamer = new fileStreamer(); streamer.fileInfo = new fileInfo(filePath); response.Content = new PushStreamContent(streamer.WritetoStream,"vIDeo/mp4"); RangeheaderValue rangeheader = Request.headers.Range; if (rangeheader != null) { long totalLength = streamer.fileInfo.Length; var range = rangeheader.Ranges.First(); streamer.Start = range.From ?? 0; streamer.End = range.To ?? totalLength - 1; response.Content.headers.ContentLength = streamer.End - streamer.Start + 1; response.Content.headers.ContentRange = new ContentRangeheaderValue(streamer.Start,streamer.End,totalLength); response.StatusCode = httpStatusCode.PartialContent; } else { response.StatusCode = httpStatusCode.OK; } return response; } class fileStreamer { public fileInfo fileInfo { get; set; } public long Start { get; set; } public long End { get; set; } public async Task WritetoStream(Stream outputStream,httpContent content,TransportContext context) { try { var buffer = new byte[65536]; using (var vIDeo = fileInfo.OpenRead()) { if (End == -1) { End = vIDeo.Length; } var position = Start; var bytesleft = End - Start + 1; vIDeo.position = Start; while (position <= End) { // what should i do here? var bytesRead = vIDeo.Read(buffer,(int)Math.Min(bytesleft,buffer.Length)); await outputStream.WriteAsync(buffer,bytesRead); position += bytesRead; bytesleft = End - position + 1; } } } catch (Exception ex) { // fail silently } finally { outputStream.Close(); } } }}
这是我的HTML代码:
<vIDeo wIDth="640" height="480" controls="controls"> <source src="/API/VIDeos/?filename=sample.mp4" type="vIDeo/mp4"> </vIDeo>
有一种方法可供我(由其他人编写)从引擎(数据库)获取一系列文件,其代码如下:
public byte[] Engine.DownloadStreamfile(GuID fileID,long from,long length)
我试图从这个方法读取并写在响应输出流,但我不能.它似乎我无法处理从谷歌铬接收.有什么想法吗?
解决方法 根据您提供的信息,并假设您的Engine类中的方法执行人们认为它通过名称和签名执行的 *** 作,您应该尝试使用Engine.DownloadStreamfile方法替换文件系统内容:// what should i do here?var bytesRead = vIDeo.Read(buffer,buffer.Length));// becomesvar bytesRead = Engine.DownloadStreamfile(this.fileID,this.Start,this.End);
您显然需要向您的类添加fileID字段/属性,而不是今天的fileInfo.
总结以上是内存溢出为你收集整理的c# – 来自数据库的asp.net web api中的视频流全部内容,希望文章能够帮你解决c# – 来自数据库的asp.net web api中的视频流所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)