2016-04-07 128 views
1

我正在寫一個ASP.NET(C#)Web應用程序,其中人們應該能夠通過FileStream從我們的SQL Server加載視頻。由於其簡單性,我正在研究使用HTML5視頻控件。我設法得到它的工作對於保存在項目中的MP4文件:HTML5視頻 - 從SQL流文件流

<video controls id="vidPlayer" runat="server" width="420"> 
    <source src="Content/Video/video.mp4" type="video/mp4"> 
    Your browser does not support HTML5 video. 
</video> 

這個工程作爲文件是存在的,但是如果我在看從一個FileStream提供的視頻,我結束了一個字節數組,我不知道如何處理它。我現在有代碼:

  SqlConnection con = new SqlConnection(constr); 
      con.Open(); 

      //Retrieve the FilePath() of the video file 
      SqlCommand sqlCommand = new SqlCommand(); 
      sqlCommand.Connection = con; 
      sqlCommand.CommandText = "SELECT EvidenceFS FROM [EP].[t_TraineeUploadedEvidence] WHERE ID = 'A5E262F8-6465-41C1-BD34-42EBCB492C87'"; 
      byte[] buffer = (byte[])sqlCommand.ExecuteScalar(); 


      MemoryStream ms = new MemoryStream(buffer); 

      vidPlayer.Src = ???? 

      //Cleanup 
      con.Close(); 

我不能幫助,但覺得我只是簡單的東西...我希望它是簡單的!我對這個領域比較陌生,所以任何幫助將不勝感激!

請讓我知道你是否需要我的更多信息!我也必須對音頻做同樣的事情,但我希望任何解決方案都應該有希望解決這兩個問題。

回答

1

我設法得到我最終需要的東西。以下是我如何這樣做 - 讚揚上述迴應,因爲他們的確幫助我指出了正確的方向!

 <video controls id="vidPlayer" runat="server" width="500" style="display:none" > 
      Your browser does not support HTML5 video. 
     </video> 

和檢索從SQL文件流數據下面的網站處理程序:那麼,視頻播放器控制,沒有源aspx文件存在

public class FileStreamHandler1 : IHttpHandler 
{ 

    public void ProcessRequest(HttpContext context) 
    { 
     int id = int.Parse(context.Request.QueryString["id"]); 
     byte[] bytes; 
     string contentType; 
     string strConnString = ConfigurationManager.ConnectionStrings["conString"].ConnectionString; 
     string name; 

     using (SqlConnection con = new SqlConnection(strConnString)) 
     { 

      using (SqlCommand cmd = new SqlCommand("[EPORTAL].[LearnerLoginGetEvidenceFile]")) 
      { 
       using (SqlDataAdapter sda = new SqlDataAdapter()) 
       { 
        cmd.CommandType = CommandType.StoredProcedure; 
        cmd.Parameters.AddWithValue("@EvidenceID", id); 
        cmd.Connection = con; 

        con.Open(); 
        SqlDataReader sdr = cmd.ExecuteReader(); 
        sdr.Read(); 
        bytes = (byte[])sdr["Data"]; 
        contentType = sdr["ContentType"].ToString(); 
        name = sdr["Name"].ToString(); 
        con.Close(); 
       } 

      } 
      context.Response.Clear(); 
      context.Response.Buffer = true; 
      context.Response.AppendHeader("Content-Disposition", "attachment; filename=" + name); 
      context.Response.ContentType = contentType; 
      context.Response.BinaryWrite(bytes); 
      context.Response.End(); 
     } 
    } 

    public bool IsReusable 
    { 
     get 
     { 
      return false; 
     } 
    } 
} 

其中記錄ID我期待for通過查詢字符串傳遞。這在代碼後面簡單地稱爲:

vidPlayer.Src = "FileStreamHandler.ashx?Id=" + SelectedEvID.ToString(); 

其中SelectedEvID取自Gridview中的選定行。

我希望這可以幫助其他人尋求實現相同。

1

這就是我使用的。

[HttpGet, Route("yourApi")] 
public HttpResponseMessage grabMyStream(string fileName) 
{ 
string mimeType = MimeMapping.GetMimeMapping(fileName); 

     MediaTypeHeaderValue mediaType = MediaTypeHeaderValue.Parse(mimeType); 

     Stream inputStream = getYourStream(); 

     var response = Request.CreateResponse(); 
     response.Content = new PushStreamContent(
      async (outputStream, httpContent, transportContext) => 
      { 
       byte[] buffer = new byte[65536]; 
       int read = 0; 
       while ((read = inputStream.Read(buffer, 0, buffer.Length)) > 0) 
       { 
        await outputStream.WriteAsync(buffer, 0, buffer.Length); 
       } 

      }, mediaType); 

     return response; 
} 

<video controls id="vidPlayer" runat="server" width="420"> 
    <source src="yourApi?fileName=video.mp4" type="video/mp4"> 
    Your browser does not support HTML5 video. 
</video> 

在傳遞文件名時使用查詢字符串,因爲末尾有一個擴展名。

編輯:getYourStream()方法是你從哪裏返回你的MemoryStream。當你的HTML加載時,HTML5視頻會向src發送一個GET請求。所以src需要指向你的API方法。這假設你正在使用Asp.Net Web Api。

注意:如果你是從某個地方一塊一塊地拉出流,那麼你會直接寫入outputStream。這是以一種方式編寫的,使您可以立即創建整個流,然後逐個流式傳輸。效率低下,但這是我項目的要求。如果你一次不需要流式傳輸,並且你可以一次獲得整個流(就像它看起來那樣),那麼我只需要從Api方法中返回整個流。

+0

謝謝BobbyJ--我不確定我是如何將你在那裏的東西轉換成我的代碼?我不能幫助,但認爲我錯過了一些核心理解,你還有什麼......這是一個功能? 'getYourStream()'是什麼?等等... – CJH

+0

我很欣賞這種迴應,儘管如此,任何其他信息,你可能會有幫助將是偉大的! – CJH

+0

好吧,這已經開始變得更有意義了..但是我的問題有點牽扯,因爲我基本上想要一個列出視頻的gridview,並且點擊Gridview中的鏈接按鈕將打開視頻控件中的視頻。我試圖讓你的建議,以適應這一點,但沒有得到太多的運氣傳遞選定的ID到Web API(加上IM與Request.CreateResponse()的問題 - 需要一個接收器的類型'HttpRequestMessage'。不知道如果有很多你可以按我的要求在那裏做的 – CJH

1

我覺得像BobbyJ,你需要製作一個外部url,你的視頻控件將加載mp4文件。例如 - 這是一個簡單的視頻控制(從這裏開始 - https://www.w3.org/wiki/HTML/Elements/video)。

<video controls 
    src="http://media.w3.org/2010/05/bunny/movie.ogv"> 
Your user agent does not support the HTML5 Video element. 
</video> 

取而代之的是「http://media.w3.org/2010/05/bunny/movie.ogv」的網址,你可以使用web服務(WCF或Web API,或任何你想要的)用URL將返回你的文件。