2013-06-01 102 views
3

我有我的視頻數據存儲在數據庫中,並希望在我的網頁中播放它。我讓自定義處理程序(FileHandler.ashx)這樣從ashx處理程序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> 

使用HTML5的視頻標籤,但它並沒有玩什麼。任何人都可以解釋我的原因嗎?

+0

任何理由,你爲什麼不接受Aristos的的答案嗎? – fubo

回答

3

主要錯誤是您沒有設置ContentTypefor the video並且您離開瀏覽器以決定它是什麼。其設置爲:

context.Response.ContentType = "video/mpeg"; 

另外,"Content-Disposition"用於下載文件,刪除也

清晰的在這裏沒有意義,刪除它

context.Response.Clear(); 

,還可以設置Buffer=off因爲你需要直接發送給瀏覽器。

這個順序沒有任何意義,只保留Flush.

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(); 
}