Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/29.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net 来自ashx处理程序的Html5视频源_Asp.net_Html_Video_Ashx - Fatal编程技术网

Asp.net 来自ashx处理程序的Html5视频源

Asp.net 来自ashx处理程序的Html5视频源,asp.net,html,video,ashx,Asp.net,Html,Video,Ashx,我有我的视频数据存储在数据库中,并希望在我的网页上播放它。我将自定义处理程序(FileHandler.ashx)设置为这样 public void ProcessRequest(HttpContext context) { int id; if (context.Request.QueryString["FileId"] == null || !Int32.TryParse(context.Request.QueryString[

我有我的视频数据存储在数据库中,并希望在我的网页上播放它。我将自定义处理程序(FileHandler.ashx)设置为这样

 public void ProcessRequest(HttpContext context)
        {
            int id;
            if (context.Request.QueryString["FileId"] == null || !Int32.TryParse(context.Request.QueryString["FileId"], out id))
                return;
            var file = lnxFile.Get(id);
            string fileName = file.Name + file.Extension;
            context.Response.Clear();
            context.Response.AddHeader("Content-Disposition", "attachment; filename=" + fileName);
            context.Response.BinaryWrite(file.Data);
            context.Response.End();
            context.Response.Flush();
        }
<video id="jwplayer_placeholder" width="320" height="240" controls> 
    <source src="<%= "/CMS/Common/FileHandler.ashx?FileId=" + id %>" type="video/mp4">  
</video>
使用html5的视频标签如下

 public void ProcessRequest(HttpContext context)
        {
            int id;
            if (context.Request.QueryString["FileId"] == null || !Int32.TryParse(context.Request.QueryString["FileId"], out id))
                return;
            var file = lnxFile.Get(id);
            string fileName = file.Name + file.Extension;
            context.Response.Clear();
            context.Response.AddHeader("Content-Disposition", "attachment; filename=" + fileName);
            context.Response.BinaryWrite(file.Data);
            context.Response.End();
            context.Response.Flush();
        }
<video id="jwplayer_placeholder" width="320" height="240" controls> 
    <source src="<%= "/CMS/Common/FileHandler.ashx?FileId=" + id %>" type="video/mp4">  
</video>


但它什么也没放。有人能给我解释一下原因吗?

主要错误是您没有设置
ContentType
,而是离开浏览器来决定它是什么。设置为:

context.Response.ContentType = "video/mpeg";
另外,
“内容处置”
也用于下载文件,请将其删除

清除在这里没有意义,请删除它

context.Response.Clear();
还可以设置
Buffer=off
,因为您需要将其直接发送到浏览器

此序列没有意义,仅保持
齐平。

   context.Response.End();
    context.Response.Flush();
因此,最终代码如下所示:

 public void ProcessRequest(HttpContext context)
{
    int id;
    if (context.Request.QueryString["FileId"] == null || !Int32.TryParse(context.Request.QueryString["FileId"], out id))
        return;
    var file = lnxFile.Get(id);

    context.Response.Buffer = false;
    context.Response.ContentType = "video/mpeg";    
    context.Response.BinaryWrite(file.Data);    
    context.Response.Flush();
}

你为什么不接受亚里士多德的答案?