0

.Net 4.5.1 - MVC5 - EF6

要求是允许大文件上传(<200MB)。我有一个带有 HttpPostedFileBase 的 ViewModel。我将其输入流设置为数据库实体非映射流属性。然后将其发送到存储库,在其中我将复制流保存到 SQLFileStream。这一切都有效。

但是...在大约 4 次大上传后进行调试时,我收到 System.OutOfMemory 异常。我可以看到 HttpRawUploadedContent 继续增长,并将不再使用的 PostedFiles 保留在内存中。我究竟做错了什么?我怎样才能得到这些东西来处理?有没有更好的办法?任何指导表示赞赏。

附件视图模型.cs

public class AttachmentViewModel
{
    #region Public Properties

    public int Id { get; set; }

    public string FileName { get; set; }

    [MaxFileSize]
    public HttpPostedFileBase PostedFile { get; set; }

    
    #endregion Public Properties
}

数据库 FileEntity 上的非映射属性

    [NotMapped]
    public Stream PostedStream {  get; set; }

FileRepository.cs 添加方法

    public override T Add(T entity)
    {
        base.Add(entity);

        //Save to generate ID
        _ctx.SaveChanges();

        using (var tx = new TransactionScope())
        {
            try
            {
                var id = entity.Id;

                var rowData =
                    _ctx.Database.SqlQuery<FileStreamData>(FileRowSelect, new SqlParameter("id", id))
                        .First();

                using (var dest = new SqlFileStream(rowData.Path, rowData.Transaction, FileAccess.Write))
                {
                    //Copy the posted stream to the SQLFileStream
                    entity.PostedStream.CopyTo(dest);
                }

                tx.Complete();
            }
            catch
            {
                //Error Uploading Stream Revert
                base.Remove(entity);
                _ctx.SaveChanges();
                throw;
            }
        }
        return entity;
    }

内存截图 在此处输入图像描述

4

1 回答 1

1

功劳应该归于@kad1r,因为他为我指出了正确的方向。(即:不是Http)

原来我不明白 requestLengthDiskThreshold 的实际作用。

RequestLengthDiskThreshold 应该小于 MaxRequestLength 值,并指示请求将在什么点或“阈值”开始透明地缓冲到磁盘上。

http://forums.asp.net/t/1680176.aspx?httpRuntime+maxRequestLength+vs+requestLengthDiskThreshold+

通过增加这个值,IIS 将更多的请求存储在内存中而不是磁盘上。

于 2015-05-15T21:37:26.913 回答