asp.net - 系统内存不足异常 : Exception when uploading a file

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

我在上传 84MB 的文件时遇到此错误(请参阅下面的错误),但当我上传 ~60MB 的文件时,它工作正常。这种情况仅发生在我们具有 4GB 内存的 32 位 2008 VM 上。在我的带有 8GB 内存的 R2 64 位虚拟机上,即使是 130MB 的文件也能正常工作。

System.OutOfMemoryException: Exception of type 'System.OutOfMemoryException' was thrown. at System.IO.BinaryReader.ReadBytes(Int32 count) at CustWeb.Controllers.DownloadsController.Create(Download dl, HttpPostedFileBase file) in c:\...\CustWeb\Controllers\DownloadsController.cs:line 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);
        }
    }

最佳答案

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

关于asp.net - 系统内存不足异常 : Exception when uploading a file,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16905723/

相关文章:

asp.net - IE 11 Request.Browser.ActiveXControls 返回 false

jquery - Ajax 不适用于 MVC 自定义路由

html - 将 anchor 标记的显示分成多行

php - 图像存储 - 更好的方式

asp.net - 如何让 Sitecore(和我的浏览器)使用打印布局?

c# - 如何将 HTML 代码放入 .resx 资源文件?

asp.net - ASP.NET 成员(member)资格是否受到 Firesheep 的保护?

asp.net-mvc - 数据库密码存储在 Web Deploy pubxml 文件中

javascript - 通过javascript提交表单时IE中的访问被拒绝错误

jquery - 使用 jquery 对话框上传文件不起作用