2010-12-09 46 views
12

我想将.flv文件存储在数据库中,而不是在文件系统中。如何从SQL数据库流式传输.flv文件

这是我现在可以做什么:
成功转换的.wmv和文件.mpeg有关ffmpeg来。FLV。
将图像存储在SQL Server中,并使用httphandler将其显示在我的页面上。
与.avi和.mpeg视频相同。 (如果他可以查看它,取决于用户的软件)
如果文件位于文件系统中而不是数据库中,则在浏览器中播放.flv文件。

什么我不能做的是:
流FLV的到JW播放器从数据库中直接的视频。 (作为二进制数据存储)

我已经在互联网上搜索了两天了,但是我无法启动它。感觉好像我几乎在那里。 JW播放器打开并开始“缓冲”,但没有任何反应。

我知道有没有简单的答案,但如果有人以前做过这个或类似的事情,我想知道你是怎么做到的。我觉得我有太多的代码在这里发布。

在此先感谢!

+4

+1恶人有趣的问题 – jcolebrand 2010-12-09 18:17:58

+0

你张贴剪断或讨论你如何流的FLV的时候它是在文件系统上? – 2010-12-09 18:55:27

+0

只需在我的回答中使用代码,但使用FileStream()代替SqlDataReader() – Niklas 2010-12-10 16:04:26

回答

4

我得到它的工作,但我不知道它有多高效。在连接,效率,负载等方面,从文件系统流式传输比从数据库流式传输更好。 我可以使用一些指针!

我使用JW玩家在这里,因此 “swfobject.js” 和 “player.swf”

的HttpHandler:

public class ViewFilm : IHttpHandler 
{ 
    public void ProcessRequest(HttpContext context) 
    { 
     try 
     { 
      // Check if id was given 
      if (context.Request.QueryString["id"] != null) 
      { 
       string movId = context.Request.QueryString["id"]; 

       // Connect to DB and get the item id 
       using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ApplicationServices"].ConnectionString)) 
       using (SqlCommand cmd = new SqlCommand("GetItem", con)) 
       { 
        cmd.CommandType = CommandType.StoredProcedure; 
        SqlParameter sqlParam = cmd.Parameters.Add("@itemId", SqlDbType.Int); 
        sqlParam.Value = movId; 

        con.Open(); 
        using (SqlDataReader dr = cmd.ExecuteReader()) 
        { 
         if (dr.HasRows) 
         { 
          dr.Read(); 
          // Add HTTP header stuff: cache, content type and length 
          context.Response.Cache.SetCacheability(HttpCacheability.Public); 
          context.Response.Cache.SetLastModified(DateTime.Now); 
          context.Response.AppendHeader("Content-Type", "video/x-flv"); 
          context.Response.AppendHeader("Content-Length", ((byte[])dr["data"]).Length.ToString()); 
          context.Response.BinaryWrite((byte[])dr["data"]); 
         } 
        } 
       } 
      } 
     } 
     catch (Exception ex) 
     { 
      throw new Exception(ex.ToString()); 
     } 
    } 

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

的JavaScript
功能增加了播放器<div id="video1">,可以调用,例如,当用户点击一个按钮。

<script type='text/javascript' src='swfobject.js'></script> 
<script type="text/javascript" language="javascript"> 
function vid() { 
    var s1 = new SWFObject('player.swf', 'player1', '480', '270', '9'); 
    s1.addParam('allowfullscreen', 'true'); 
    s1.addParam('allowscriptaccess', 'always'); 
    s1.addVariable('file', encodeURIComponent('ViewFilm.ashx?id=10')); 
    s1.addVariable('type', 'video'); 
    s1.write(document.getElementById("video1")); 
} 
</script> 
2

不确定从字面上直接从数据库中“直接流”,但它将工作将JW播放器的源“文件”设置为“ServeFLV.aspx?id = 123”,并具有ServeFLV.aspx从数据库中检索字节,并将它们写出到没有标记的响应中?