上传期间文件大小超过默认大小

问题描述:

我正在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

可能重复(http://stackoverflow.com/questions/3853767/maximum-request-length-exceeded) – 2013-05-06 16:33:40

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

<httpRuntime maxRequestLength="NNN" /> 

其中nnn是文件大小以KB为单位

+0

我发现错误在下面的代码中,当从浏览器请求“。 ' – 2013-05-06 16:32:13

+1

@FakharuzZaman:你做错了。那里应该只有一个httpRuntime节点。如 2013-05-06 16:41:09

web.config文件system.web下添加这些设置:

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

maxRequestLength is in kb and executionTimeout in 分钟。你应该设置executionTimeout给它足够的时间来上传文件。

查询this了解更多详情。

+0

IIS会考虑您推荐的代码错误。 – 2013-05-06 16:35:11

+0

@FakharuzZaman:什么是错误? – 2013-05-06 16:39:26

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

<location path="Documents/Upload"> 
    <system.web> 
    <httpRuntime maxRequestLength="51200" /> 
    </system.web> 
</location> 
的[超过最大请求长度]
+0

提到了一个好点:) – 2013-05-06 16:56:50