【问题标题】:System.OutOfMemoryException: Exception when uploading a fileSystem.OutOfMemoryException:上传文件时出现异常
【发布时间】:2013-05-30 03:13:15
【问题描述】:

我在上传 84MB 的文件时遇到此错误(请参阅下面的错误),但当我上传 ~60MB 的文件时,它可以正常工作。这只发生在我们的 32 位 2008 虚拟机 w/4GB 内存上。在我的带有 8GB 内存的 R2 64 位 VM 上,它甚至可以处理 130MB 的文件。

System.OutOfMemoryException:类型异常 'System.OutOfMemoryException' 被抛出。在 System.IO.BinaryReader.ReadBytes(Int32 计数)在 CustWeb.Controllers.DownloadsController.Create(下载 dl, HttpPostedFileBase 文件)在 c:\...\CustWeb\Controllers\DownloadsController.cs:第 72 行

不过,我在任务管理器中监控了内存,在整个上传过程中它从未超过 74%。

这是一个基于 4.5 .NET 框架的 MVC 4 应用程序。

我的 web.config 中有处理上传文件的最大设置。

<httpRuntime requestValidationMode="4.5" targetFramework="4.5" maxRequestLength="2147483647" executionTimeout="84000" />

...

<security>
     <requestFiltering>
         <requestLimits maxAllowedContentLength="2147482624" />
     </requestFiltering> 
</security>

更新按要求添加代码:

    public ActionResult Create(Download dl, HttpPostedFileBase file)
    {
        try
        {
            using (var binaryReader = new BinaryReader(file.InputStream))
            {
                dl.FileBytes = binaryReader.ReadBytes(file.ContentLength);
            }
            dl.FileContentType = file.ContentType;
            dl.FileName = file.FileName;
            dl.Insert();
            Success("<strong>" + dl.Label + "</strong> created and uploaded successfully.");
            return RedirectToAction("Index");
        }
        catch (Exception ex)
        {
            SelectList list = new SelectList(new DownloadType().GetDownloadTypes(), "ID", "Name");
            ViewBag.DownloadTypeList = list;
            Error(ex.ToString());
            return View(dl);
        }
    }

【问题讨论】:

  • 在这里使用BinaryReader 是错误的。
  • @SLaks - 按照您的要求添加了我的代码。
  • @leppie 我添加了我的代码 - 有没有比使用 BinaryReader 更好的方法来做我正在做的事情?
  • @leppie - 我将它从 BinaryReader 更改为仅使用 MemoryStream ,但仍然出现相同的错误。

标签: asp.net asp.net-mvc file-upload


【解决方案1】:

默认情况下,ASP.NET 允许使用 60% 的可用内存 - 检查 memoryLimit in processModel in machine.config。您可以将该值更改为 80%,以便为 ASP.NET 提供更多空间,但不建议这样做。考虑其他选项,例如分块上传文件等。

【讨论】:

  • 哇——你如何分块上传文件?
  • 从使用 HTTP 模块例如 dotnetslackers.com/Community/blogs/haissam/archive/2008/09/12/… 到使用 3rd 方控件例如有不同的方式plupload.com。但甚至在此之前尝试使用上面显示的 web.config 的httpRuntime 部分的requestLengthDiskThreshold 属性。它控制上传文件的大小,服务器可以在将文件缓存到磁盘之前将其保存在内存中。
  • 默认显示为 256 字节,以防止使用过多内存。似乎增加它会使用更多的内存并使问题变得更糟,不是吗?
  • 对于 .NET 4.5,默认值为 80 KB,但您说得对,增加会使情况变得更糟。那就不要介意这个属性,不幸的是它不会帮助你。我的想法是如果默认值太大 - 尝试减少它,但事实并非如此
【解决方案2】:

我将它从 stream 更改为 file.SaveAs 并防止了内存错误。

    public ActionResult Create(Download dl, HttpPostedFileBase file)
    {
        try
        {
            string tempFile = Path.Combine(Server.MapPath("~/App_Data/"), string.Format("{0}_{1}",Guid.NewGuid().ToString(), Path.GetFileName(file.FileName)));
            file.SaveAs(tempFile);
            dl.FileBytes = System.IO.File.ReadAllBytes(tempFile);
            dl.FileContentType = file.ContentType;
            dl.FileName = file.FileName;
            dl.Insert();
            System.IO.File.Delete(tempFile);
            Success("<strong>" + dl.Label + "</strong> created and uploaded successfully.");
            return RedirectToAction("Index");
        }
        catch (Exception ex)
        {
            SelectList list = new SelectList(new DownloadType().GetDownloadTypes(), "ID", "Name");
            ViewBag.DownloadTypeList = list;
            Error(ex.ToString());
            return View(dl);
        }
    }

【讨论】:

  • 我仍然偶尔会遇到内存错误,但将其更改为这种方法肯定会改善(加倍)我能够上传的文件大小。我想我必须增加实际 RAM 才能比 160MB 文件做得更好。
  • 在阅读了这个主题之后。我的问题不是文件的上传,而是在将其保存到数据库之前的 readAllBytes 。看起来如果我可能要保存 200MB 以上的文件,我应该将它们存储在文件系统中并保存对数据库的引用。
猜你喜欢
  • 1970-01-01
  • 2021-12-25
  • 2015-06-03
  • 2011-09-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-03-04
相关资源
最近更新 更多