我得到它的工作,但我不知道它有多高效。在连接,效率,负载等方面,从文件系统流式传输比从数据库流式传输更好。 我可以使用一些指针!
我使用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>
+1恶人有趣的问题 – jcolebrand 2010-12-09 18:17:58
你张贴剪断或讨论你如何流的FLV的时候它是在文件系统上? – 2010-12-09 18:55:27
只需在我的回答中使用代码,但使用FileStream()代替SqlDataReader() – Niklas 2010-12-10 16:04:26