c# - 上传文件时OutOfMemory

标签 c# asp.net file-upload plupload

我的代码:

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,为什么我的内存不足?是否有使用更少内存的替代方法?

最佳答案

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

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

关于c# - 上传文件时OutOfMemory,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17494564/

相关文章:

c# - 带有 DisableRequestSizeLimit 的 ASP.NET Core 测试方法

asp.net - 以编程方式在 ASPX 页面中为 'visible' 属性设置值

c# - 如何在 asp.net 中将 RDLC 报告导出为 .xlsx 格式?

AngularJS:文件上传

c# - 在wpf应用程序中读取excel文件内容

c# - 如何使用多个 GridView ItemTemplate (WinRT/XAML)

c# - AJAXToolkit动态隐藏选项卡?

javascript - 使用jquery获取表中隐藏标签的值?

ruby-on-rails - Ruby On Rails - 如何重命名当前上传的文件?

jquery - BlueImp 文件上传 "Error: Method Not Allowed"- Laravel 4 路由问题