为System.OutOfMemoryException:上传文件时异常 [英] System.OutOfMemoryException: Exception when uploading a file

查看:873
本文介绍了为System.OutOfMemoryException:上传文件时异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我上传的文件是84MB,当收到此错误(见下面的错误),但是当我上传了〜60MB的文件,它工作正常。这仅发生在我们2008年的32位虚拟机瓦特/ 4GB的内存。在我的64位R2虚拟机瓦特/ 8GB内存,它工作正常使用甚至130MB的文件。


  

的System.OutOfMemoryException:类型的异常
  'System.OutOfMemoryException的'被抛出。在
  System.IO.BinaryReader.ReadBytes(的Int32数)的
  CustWeb.Controllers.DownloadsController.Create(下载DL,
  HttpPostedFileBase文件)中
  C:\\ ... \\ CustWeb \\ \\控制器DownloadsController.cs:行72


我监视在任务管理器的内存,虽然,在整个上传过程中它永远不会74%以上。

这是在.NET 4.5框架的MVC应用4。

我在我的web.config最高设置在处理上传文件。

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

...

 <安全>
     <&的requestFiltering GT;
         < requestLimits maxAllowedContentLength =2147482624/>
     < /&的requestFiltering GT;
< /安全>

更新添加$ C $按要求C:

 公众的ActionResult创建(下载DL,HttpPostedFileBase文件)
    {
        尝试
        {
            使用(VAR binaryReader =新BinaryReader(file.InputStream))
            {
                dl.FileBytes = binaryReader.ReadBytes(file.ContentLength);
            }
            dl.FileContentType = file.ContentType;
            dl.FileName = file.FileName;
            dl.Insert();
            成功(<强>中+ dl.Label +< / STRONG方式>创建并上传成功);
            返回RedirectToAction(「指数」);
        }
        赶上(异常前)
        {
            的SelectList名单=新的SelectList(新DownloadType()GetDownloadTypes(),ID,名);
            ViewBag.DownloadTypeList =清单;
            误差(ex.ToString());
            返回查看(DL);
        }
    }


解决方案

我改变了它从流file.SaveAs和prevented的内存错误。

 公众的ActionResult创建(下载DL,HttpPostedFileBase文件)
    {
        尝试
        {
            字符串TEMPFILE = Path.Combine(使用Server.Mappath(〜/ App_Data文件/)的String.Format({0} _ {1},Guid.NewGuid()。toString()方法,Path.GetFileName(file.FileName )));
            file.SaveAs(临时文件);
            dl.FileBytes = System.IO.File.ReadAllBytes(临时文件);
            dl.FileContentType = file.ContentType;
            dl.FileName = file.FileName;
            dl.Insert();
            System.IO.File.Delete(临时文件);
            成功(<强>中+ dl.Label +< / STRONG方式>创建并上传成功);
            返回RedirectToAction(「指数」);
        }
        赶上(异常前)
        {
            的SelectList名单=新的SelectList(新DownloadType()GetDownloadTypes(),ID,名);
            ViewBag.DownloadTypeList =清单;
            误差(ex.ToString());
            返回查看(DL);
        }
    }

I'm getting this error when uploading a file that is 84MB (see error below) but when I upload a ~60MB file, it works fine. This only occurs on our 32bit 2008 VM w/ 4GB memory. On my R2 64bit VM w/8GB memory, it works fine with even 130MB file.

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

I monitored the Memory in the task manager, though, and it never goes above 74% during the entire upload process.

This is an MVC 4 application on the 4.5 .NET framework.

I have the max settings in my web.config in dealing with uploading files.

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

...

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

UPDATE Adding code as requested:

    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);
        }
    }

解决方案

I changed it from stream to file.SaveAs and that prevented the memory error.

    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);
        }
    }

这篇关于为System.OutOfMemoryException:上传文件时异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆