1

我正在使用下面的代码从我的 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; }
    }
}
4

1 回答 1

0

第一:不要从数据库中流式传输。

第二:如果你真的想从数据库中流式传输,这里是你可以如何做的例子。但是不要。真的。在数据库中存储指针(文件路径)并将流处理留给 ISS 或您使用的任何服务器。

于 2011-03-08T17:24:32.083 回答