出于性能考虑,我使用SqlConnection和SqlReaderStream从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 action中使用此ASP.NET方法。
[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附加信息:
Get操作。所有其他操作正常加载。FilesController (其中包含Get操作)直接从Controller类继承
问题的可能原因是什么?
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);
    }
}发布于 2015-08-18 20:38:56
我不会在SqlReaderStream中返回FileStreamResult,因为流不是可查找的。我想这可能是个问题。
尝试将Stream复制到MemoryStream或字节数组,然后在GetFileById中返回它。
此外,如果在本地读取SqlReaderStream函数,则可以关闭到数据库的连接。
return File(byteArray, contentType, name);https://stackoverflow.com/questions/31677242
复制相似问题