我在上传 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/