2

我在 ASP.NET MVC 中工作。我正在尝试上传文件,在小文件的情况下成功,但如果文件很大,我会收到以下错误。

Maximum request length exceeded.

以下是我的表格。

@using (@Html.BeginForm("Method","Controller",FormMethod.Post,new{enctype="multipart/form-data"}))
{
   <input type="file" name="file" id="file"/>
   <input type="submit" value="Submit"/>
}

以下是控制器方法。

[HttpPost]
public ActionResult Method(HttpFileBase file)
{
    string path = System.IO.Path.Combine(Server.MapPath("~/Files"), file.FileName);

    file.SaveAs(path);
}

当文件较小(即 1-2Mb)时,相同的代码可以正常工作,但我正在尝试上传未上传的 19Mb 文件。我不知道如何指定文件长度以及如何删除上述错误。请帮我。

4

3 回答 3

1

在 System.Web 节点下的 web.config 中指定

<httpRuntime maxRequestLength="NNN" />

其中 NNN 是以千字节为单位的文件大小

于 2013-05-06T16:22:53.760 回答
1

在您的web.config文件中system.web添加以下设置:

<system.web>
  ....
   <httpRuntime maxRequestLength="XXXX" executionTimeout="XXXX" />
  ....
</system.web>

maxRequestLengthkb为单位,executionTimeout分钟为单位。你应该设置executionTimeout给它足够的时间来上传文件。

检查以获取更多详细信息。

于 2013-05-06T16:23:59.940 回答
1

您可以按照此处的建议使用 web.config 更改最大上传大小,但我建议将该更改绑定到您希望将其上传到的确切 URL。例如,这会将最大上传大小更改为 50MB。

<location path="Documents/Upload">
  <system.web>
    <httpRuntime maxRequestLength="51200" />
  </system.web>
</location>
于 2013-05-06T16:54:24.620 回答