我正在使用下面的代码从我的 SQL Server 流式传输 .flv 视频。但据我了解,整个视频将在播放之前加载到内存中。我想做的是将CommandBehavior.SequentialAccess 添加到SQLDataReader ...但我无法让它工作。
请在这里帮助我。如果你能提供一个适用于我的工作示例,我会很高兴。伪代码将是第二好的选择。不过,任何指针都值得赞赏。
这是我的 httpHandler
public class ViewFilm : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
try
{
// Check if video id was given
if (context.Request.QueryString["id"] != null)
{
string video_ID = context.Request.QueryString["id"];
// Connect to DB and get the video
using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ApplicationServices"].ConnectionString))
using (SqlCommand cmd = new SqlCommand("GetVideo", con))
{
cmd.CommandType = CommandType.StoredProcedure;
SqlParameter sqlParam = cmd.Parameters.Add("@videoId", SqlDbType.Int);
sqlParam.Value = video_ID;
con.Open();
using (SqlDataReader dr = cmd.ExecuteReader())
{
if (dr.HasRows)
{
dr.Read();
// Add HTTP header: 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; }
}
}