【发布时间】: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