如何从SQL数据库流式传输.flv文件
-
10-10-2019 - |
题
我想将.flv文件存储在数据库中,而不是在文件系统中。
这就是我现在可以做的:
使用FFMPEG成功将.WMV和.MPEG转换为.flv。
将图像存储在SQL Server中,并在我的页面上显示Httphandler。
与.avi和.mpeg视频相同。 (如果可以查看的话,这取决于用户的软件)
如果文件位于文件系统中而不是数据库中,则在浏览器中播放.flv文件。
我做不到的是:
直接从数据库中将.FLV视频传输到JW播放器。 (存储为二进制数据)
我已经搜索了两天的互联网,但我无法正常工作。感觉好像我快到了。 JW播放器打开并开始“缓冲区”,但什么也没有发生。
我知道没有简单的答案,但是如果有人以前做过此事,或者类似的事情,我想知道您的表现。我觉得我有太多代码在这里发布。
提前致谢!
解决方案
我开始工作,但我不知道它的效率有多高。从文件系统流式传输比从数据库中流式传输更好,在连接,效率,负载等方面,我可以在此上使用一些指针!
我在这里使用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>
其他提示
不确定如何从字面上看“直接从数据库中流”,但是为JW播放器设置源“ file”是否有效,以“ serveflv.aspx?id = 123”,并使Serveflv.aspx从数据库,并没有标记将其写入响应?
如果您使用的是SQL Server 2008,则可以使用 varbinary(max)fileestream 这将允许文件由数据库管理,但仍然使您可以访问 来自.NET的FILESTREAS.
不隶属于 StackOverflow