【问题标题】:OutOfMemory when uploading files上传文件时出现内存不足
【发布时间】:2013-07-06 02:22:27
【问题描述】:

我的代码:

int chunk = Request["chunk"] != null ? int.Parse(Request["chunk"]) : 0;
string fileName = Request["name"] != null ? Request["name"] : string.Empty;
Response.Write(Request.Files.Count);
HttpPostedFile fileUpload = Request.Files[0];

var uploadPath = Server.MapPath("~/uploaded-files");
if (Request.QueryString["originals"] == "yes")
{
    using (var fs = new FileStream(Path.Combine(uploadPath, fileName), chunk == 0 ? FileMode.Create : FileMode.Append))
    {
        var buffer = new byte[fileUpload.InputStream.Length];
        fileUpload.InputStream.Read(buffer, 0, buffer.Length);
        fs.Write(buffer, 0, buffer.Length);
    }
}

我收到“System.OutOfMemoryException”错误。

我的机器有 4 GB 的 RAM。有问题的文件大约为 1 GB。我正在使用 plUpload 进行上传。

我已启用 3GB 开关。没有不同。我有很多可用的 RAM,为什么我的内存不足?有没有使用更少内存的替代方法?

【问题讨论】:

  • 分块读取和写入,而不是一次全部写入?
  • 尝试为 x64 平台目标编译。
  • 您是否在配置中弄乱了 httpRuntime 设置? HttpPostedFile 应该在超过 256KB 时缓冲到磁盘,除非您提高了 requestLengthDiskThreshold。
  • 我要研究块上传

标签: c# asp.net file-upload plupload


【解决方案1】:

另一种实现是在 HttpPostedFile 上使用 SaveAs

fileUpload.SaveAs(Path.Combine(uploadPath, fileName));

【讨论】:

  • 太棒了!这可以解决问题,我仍将研究块上传,但现在可行!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-01-13
  • 2021-04-23
  • 1970-01-01
  • 2011-09-18
相关资源
最近更新 更多