1

按照我的代码:

[HttpGet]
public ActionResult StreamVideo(int type1, int type2)
{
    using (var ctx = new Entities())
    {
        var result = ctx.Table.Where(x => x.Type1 == type1 && x.Type2 == type2).FirstOrDefault();

        byte[] video_byte = result.Video;

        return new RangeFileContentResult(video_byte, "video/x-msvideo", "NameFile.mp4", DateTime.Now);
    }
}

我有一个“模态引导程序”,其中包含视频内容。关闭模态并再次打开时,会出现问题:

System.OutOfMemoryException:'Exception_WasThrown'

在此处输入图像描述

线上出现问题:

var result = ctx.Table.Where(x => x.Type1 == type1 && x.Type2 == type2).FirstOrDefault();

有什么解决办法吗?

4

1 回答 1

1

在检索大量varbinary数据时,您需要小心不要使大对象堆负担过重。考虑改为以流的形式检索数据。 EntityCommand并且SqlCommand都可以检索读者,你可以从他们那里得到一个流。

SqlClient 流式传输

using (connection)
{
    SqlCommand command = new SqlCommand(
      $"SELECT Video FROM Table where Type1={type1} and Type2={type2};",
      connection);
    connection.Open();

    SqlDataReader reader = command.ExecuteReader();

    reader.Read();
    var stream = reader.GetStream(0);

   ... Use the stream here...
}
于 2017-10-17T20:22:57.483 回答