c# - HttpException - Chrome 中超出了最大请求长度

标签 c# asp.net-mvc-3 file-upload

我正在使用 ASP.NET MVC 3 开发 uploader 。我正在使用 AJAX 上传控件 (http://valums.com/ajax-upload/)。当我尝试通过 IE 上传大文件时,一切正常。但是,当我尝试通过 Chrome 上传大文件时,服务器出现异常,提示“超出最大长度”。我添加了以下配置设置,认为它可以解决它:

<httpRuntime maxRequestLength="2147483647" executionTimeout="3600" />

但这并没有解决问题。我的操作代码如下所示:

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult UploadFiles(IEnumerable<HttpPostedFileBase> files)
{
  string home = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "files");
  if (String.IsNullOrEmpty(Request["qqFile"]))
  {
    string filename = string.Empty;
    foreach (string file in Request.Files)
    {
      HttpPostedFileBase postedFile = (HttpPostedFileBase)(Request.Files[file]);
      if (postedFile.ContentLength == 0)
        continue;

      filename = Path.Combine(home, Path.GetFileName(postedFile.FileName));
      if (Directory.Exists(baseDirectory) == false)
        Directory.CreateDirectory(baseDirectory);
      postedFile.SaveAs(filename);
    }
  }
  else
  {
    // MY PROBLEM IS IN HERE
    string filename = Path.Combine(baseDirectory, Request["qqFile"]);

    byte[] buffer = new byte[Request.InputStream.Length];
    Request.InputStream.Read(buffer, 0, buffer.Length);
    System.IO.File.WriteAllBytes(filename, buffer);
  }

  return Json(new { success = true }, "text/html");
}

为什么我不能通过 Chrome 上传更大的文件?

最佳答案

您可以尝试设置 maxAllowedContentLength

<system.webServer>
    <security>
        <requestFiltering>
             <requestLimits maxAllowedContentLength="X" />
        </requestFiltering>
    </security>
</system.webServer>

关于c# - HttpException - Chrome 中超出了最大请求长度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8856212/

相关文章:

java - HTTPServlet Request.getInputStream() 总是接收空行

c# - Linq to SQL、InsertOnSubmit 与 InsertAllOnSubmit 性能对比?

c# - 使用 OData V4 客户端更新动态实体

javascript - 如何在我们的 mvc3 web 应用程序的产品构建中删除 JS 日志记录调用?

c# - MVC3 扩展方法 <TModel, TValue>

PHP文件上传在本地工作但不在服务器上工作

file-upload - 如何处理基于 s3 浏览器的上传?

c# - 表示层、业务层和数据层

c# - 尝试将属性路由与 autofac 一起使用的异常

asp.net-mvc - 无法在 SimpleInjectorInitializer 中检索用于验证的 Entity Framework 对象上下文