我已经让它工作了,但我不知道它的效率如何。从连接、效率和负载等方面来看,从文件系统流还是从数据库流更好呢?我需要一些指导!
我在这里使用JW Player,因此需要使用“swfobject.js”和“player.swf”。
httpHandler:
public class ViewFilm : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
try
{
if (context.Request.QueryString["id"] != null)
{
string movId = context.Request.QueryString["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();
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>