【发布时间】:2015-10-19 01:32:22
【问题描述】:
出于性能考虑,我使用SqlConnection 和SqlReaderStream 从SQL Server 数据库返回byte[] 流:
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;
}
我在 ASP.NET MVC 操作中使用了这个 GetFileById 方法:
[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 FileStreamResult 与 SqlReaderStream 连接起来:
return new FileStreamResult(file.Content, contentType);
当我尝试使用 Chrome(或 Firefox...)加载资源时加载了整个文件,但出现以下错误:
注意:请求尚未完成!
响应标头:
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类 - 文件加载成功但浏览器仍在等待服务器:
- 我在使用 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);
}
}
【问题讨论】:
-
您能发布
SqlReaderStream的代码吗?问题可能在这个类中。 -
当然:)。查看我编辑的帖子。
-
你的
[Content]字段真的包含33429字节的数据吗?或者它有更少?执行时得到什么值:reader.GetBytes(2, 0, null, 0, 0);? -
我认为
FileStreamResult不可能知道流已经结束。您没有返回Length,也没有EndOfStream属性 -
试试
this.reader.GetBytes( this.columnIndex, offset, buffer, 0, count);
标签: c# asp.net-mvc http filestream sqlconnection