gpt4 book ai didi

c# - 使用 ASP.NET MVC 在 FileStreamResult 中使用 SqlDataReader 时请求未完成

转载 作者:太空狗 更新时间:2023-10-29 20:20:09 28 4
gpt4 key购买 nike

出于性能考虑,我使用 SqlConnectionSqlReaderStream用于返回 byte[] 来自 SQL Server 数据库的流:

private static SqlConnection GetConnection()
{
var sqlConnectionStringBuilder =
new SqlConnectionStringBuilder(
ConfigurationManager.ConnectionStrings["StudentsSystemEntities"].ConnectionString)
{
Pooling = true
};
var connection = new SqlConnection(sqlConnectionStringBuilder.ConnectionString);
connection.Open();
return connection;
}

public FileDownloadModel GetFileById(Guid fileId)
{
var connection = GetConnection();
var command = new SqlCommand(
@"SELECT [FileSize], [FileExtension], [Content] FROM [dbo].[Files] WHERE [FileId] = @fileId;",
connection);
var paramFilename = new SqlParameter(@"fileId", SqlDbType.UniqueIdentifier) { Value = fileId };
command.Parameters.Add(paramFilename);

var reader = command.ExecuteReader(
CommandBehavior.SequentialAccess | CommandBehavior.SingleResult
| CommandBehavior.SingleRow | CommandBehavior.CloseConnection);

if (reader.Read() == false) return null;

var file = new FileDownloadModel
{
FileSize = reader.GetInt32(0),
FileExtension = reader.GetString(1),
Content = new SqlReaderStream(reader, 2)
};
return file;
}

我正在使用这个 GetFileById ASP.NET MVC 操作中的方法:

[HttpGet]
public ActionResult Get(string id)
{
// Validations omitted

var file = this.filesRepository.GetFileById(guid);

this.Response.Cache.SetCacheability(HttpCacheability.Public);
this.Response.Cache.SetMaxAge(TimeSpan.FromDays(365));
this.Response.Cache.SetSlidingExpiration(true);

this.Response.AddHeader("Content-Length", file.FileSize.ToString());
var contentType = MimeMapping.GetMimeMapping(
string.Format("file.{0}", file.FileExtension));
// this.Response.BufferOutput = false;
return new FileStreamResult(file.Content, contentType);
}

我正在连接 MVC FileStreamResultSqlReaderStream在以下行中:

return new FileStreamResult(file.Content, contentType);

当我尝试使用 Chrome(或 Firefox...)加载资源时,已加载整个文件,但出现以下错误:

CAUTION: request is not finished yet!

CAUTION: request is not finished yet!

响应 header :

HTTP/1.1 200 OK
Cache-Control: public, max-age=31536000
Content-Length: 33429
Content-Type: image/png
Server: Microsoft-IIS/10.0
X-AspNetMvc-Version: 5.2
X-AspNet-Version: 4.0.30319
X-SourceFiles: =?UTF-8?B?QzpcR---trimmed---G5n?=
X-Powered-By: ASP.NET
Date: Tue, 28 Jul 2015 13:02:55 GMT

附加信息:

  • 我没有使用任何 Chrome 扩展程序
  • 问题仅出在给定的 Get 上行动。所有其他操作正在正常加载
  • FilesController (其中 Get 操作)直接继承自 Controller
  • 文件加载成功但浏览器仍在等待服务器:enter image description here
  • 我在使用 Firefox 时遇到的问题完全相同

问题的可能原因是什么?

SqlReaderStream 的源代码类

public class SqlReaderStream : Stream
{
private readonly int columnIndex;

private SqlDataReader reader;

private long position;

public SqlReaderStream(
SqlDataReader reader,
int columnIndex)
{
this.reader = reader;
this.columnIndex = columnIndex;
}

public override long Position
{
get { return this.position; }
set { throw new NotImplementedException(); }
}

public override bool CanRead
{
get { return true; }
}

public override bool CanSeek
{
get { return false; }
}

public override bool CanWrite
{
get { return false; }
}

public override long Length
{
get { throw new NotImplementedException(); }
}

public override void Flush()
{
}

public override int Read(byte[] buffer, int offset, int count)
{
var bytesRead = this.reader.GetBytes(
this.columnIndex, this.position, buffer, offset, count);
this.position += bytesRead;
return (int)bytesRead;
}

public override long Seek(long offset, SeekOrigin origin)
{
throw new NotImplementedException();
}

public override void SetLength(long value)
{
throw new NotImplementedException();
}

public override void Write(byte[] buffer, int offset, int count)
{
throw new NotImplementedException();
}

protected override void Dispose(bool disposing)
{
if (disposing && null != this.reader)
{
this.reader.Dispose();
this.reader = null;
}

base.Dispose(disposing);
}
}

最佳答案

我不会返回 SqlReaderStreamFileStreamResult ,因为流不是可搜索的。我想这可能是个问题。

尝试复制 StreamMemoryStreambyte array 并在 GetFileById 中返回它相反。

此外,如果您阅读 SqlReaderStream,您可以关闭与数据库的连接。局部功能。

return File(byteArray, contentType, name);

关于c# - 使用 ASP.NET MVC 在 FileStreamResult 中使用 SqlDataReader 时请求未完成,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31677242/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com