超出最大请求长度.

Sur*_*har 1008 asp.net iis file-upload

当我尝试在我的网站上传视频时,我收到错误最大请求长度超出.

我该如何解决?

Sac*_*hag 1902

如果您使用IIS来托管应用程序,则默认上载文件大小为4MB.要增加它,请在web.config中使用以下部分 -

<configuration>
    <system.web>
        <httpRuntime maxRequestLength="1048576" />
    </system.web>
</configuration>
Run Code Online (Sandbox Code Playgroud)

对于IIS7及更高版本,您还需要添加以下行:

 <system.webServer>
   <security>
      <requestFiltering>
         <requestLimits maxAllowedContentLength="1073741824" />
      </requestFiltering>
   </security>
 </system.webServer>
Run Code Online (Sandbox Code Playgroud)

注意:

  • maxRequestLength千字节为单位
  • maxAllowedContentLength字节为单位

这就是为什么这个配置示例中的值不同的原因.(两者相当于1 GB)

  • 使用IIS 7.5和VS RC 2012 IIS Express,我必须同时设置这些.httpRuntime配置ASP.NET的最大长度,而requestLimits配置IIS的最大长度,http://stackoverflow.com/questions/6327452/which-gets-priority-maxrequestlength-or-maxallowedcontentlength和http://forums.iis.net/吨/ 1169846.aspx (31认同)
  • 值得指出的是,maxAllowedContentLength以字节为单位,而不是千字节.这两个值不应该是相同的数字,因为它们不是相同的测量单位. (18认同)
  • 确保将此设置添加到主`Web.config`而不是`Views`文件夹中 (12认同)
  • maxAllowedContentLength必须以字节为单位,而不是千字节,因此提供的示例不等效. (11认同)
  • 谢谢sachin,我添加了一些像<httpRuntime maxRequestLength ="32768"executionTimeout ="180"/> (7认同)
  • @SachinShanbhag请包括`重要提示:这两个值必须匹配.在这种情况下,我的最大上传量为1024兆字节.**来自Karls回答**以使其正确,然后它可以工作. (2认同)
  • 您的计算以 Kibibytes 和 Gibibyte 为单位,是时候更新答案了?呵呵。 (2认同)

Kar*_*arl 542

我认为这里没有提到它,但为了使其工作,我必须在web.config中提供这两个值:

system.web

<httpRuntime maxRequestLength="1048576" executionTimeout="3600" />
Run Code Online (Sandbox Code Playgroud)

并在 system.webServer

<security>
    <requestFiltering>
        <requestLimits maxAllowedContentLength="1073741824" />
    </requestFiltering>
</security>
Run Code Online (Sandbox Code Playgroud)

重要提示:这两个值必须匹配.在这种情况下,我的最大上传量为1024兆字节.

maxRequestLength有1048576 KILOBYTES,maxAllowedContentLength有1073741824 BYTES.

我知道这很明显,但很容易被忽视.

  • 我必须将它与现有的行结合起来:<httpRuntime targetFramework ="4.5"maxRequestLength ="1048576"executionTimeout ="3600"/> (25认同)
  • 对于它可能涉及的任何问题:这个答案也适用于IIS-Express(带WebMatrix的asp.net-webpages) (5认同)
  • 是的,这个答案对我有用,而不是萨钦的答案.它也适用于Azure. (5认同)
  • 这绝对是正确答案......两个条目必须存在.对于MVC 3,它可以在项目根目录`web.config`文件中. (4认同)
  • 另一个重要的事情是卡尔提到的'executionTimeout' (2认同)
  • 对于所有不熟悉 ASP.NET 的人,都有一个 web.config 文件和一个 web.Debug.config 文件。编辑需要进入 Web.Config 文件,而不是 web.Debug.config 文件。 (2认同)

Nic*_*cht 192

值得注意的是,您可能希望将此更改限制为您希望用于上载的URL,而不是整个站点.

<location path="Documents/Upload">
  <system.web>
    <!-- 50MB in kilobytes, default is 4096 or 4MB-->
    <httpRuntime maxRequestLength="51200" />
  </system.web>
  <system.webServer>
    <security>
      <requestFiltering>
        <!-- 50MB in bytes, default is 30000000 or approx. 28.6102 Mb-->
        <requestLimits maxAllowedContentLength="52428800" /> 
      </requestFiltering>
    </security>
  </system.webServer>
</location>
Run Code Online (Sandbox Code Playgroud)

  • 老兄,你摇滚.对不起,但我的小小投票还不够. (2认同)

Ser*_*ltz 43

并且万一有人正在寻找一种方法来处理这个异常并向用户显示一个有意义的解释(例如"你上传的文件太大了"):

//Global.asax
private void Application_Error(object sender, EventArgs e)
{
    var ex = Server.GetLastError();
    var httpException = ex as HttpException ?? ex.InnerException as HttpException;
    if(httpException == null) return;

    if(httpException.WebEventCode == WebEventCodes.RuntimeErrorPostTooLarge)
    {
        //handle the error
        Response.Write("Too big a file, dude"); //for example
    }
}
Run Code Online (Sandbox Code Playgroud)

(需要ASP.NET 4或更高版本)


Dav*_*ave 28

默认情况下,最大请求大小为4mb(4096 KB)

这在此解释:http://support.microsoft.com/default.aspx?scid = kb; EN-US; 2955626

上面的文章还解释了如何解决这个问题:)

  • 该链接将我重定向到Microsoft支持主页 (4认同)

Ser*_*sov 20

如果您无法更新配置文件但控制处理文件上载使用的代码HttpContext.Current.Request.GetBufferlessInputStream(true).

参数的truedisableMaxRequestLength告诉框架忽略已配置的请求限制.

有关详细说明,请访问https://msdn.microsoft.com/en-us/library/hh195568(v=vs.110).aspx


ema*_*ema 19

There's an element in web.config to configure the max size of the uploaded file:

<httpRuntime 
    maxRequestLength="1048576"
  />
Run Code Online (Sandbox Code Playgroud)


Ber*_*eSF 9

在一个地方总结所有答案:

<system.web>
  <httpRuntime targetFramework="4.5.2" maxRequestLength="1048576"/>
</system.web>

<system.webServer>
  <security>
    <requestFiltering>
      <requestLimits maxAllowedContentLength="1073741824" />
    </requestFiltering>
  </security>
</system.webServer>
Run Code Online (Sandbox Code Playgroud)

规则:

  • maxRequestLength(以kb表示)值必须与maxAllowedContentLength匹配(以字节表示).
  • 大多数情况下,你的system.web部分可能已包含"httpRuntime".将targetFramework设置为您使用的.net版本.

笔记:

  • maxRequestLength的默认值是4096(4mb).最大值为2,147,483,647
  • maxAllowedContentLength的默认值是30,000,000(约30mb).最大值为4,294,967,295

更多信息MSDN


Uni*_*der 8

maxRequestLength(以KB为单位的长度)此处为ex.我拿1024(1MB)maxAllowedContentLength(字节长度)应该与你的maxRequestLength(1048576字节= 1MB)相同.

<system.web>
   <httpRuntime maxRequestLength="1024" executionTimeout="3600" />
</system.web>

<system.webServer>
   <security>
      <requestFiltering>
          <requestLimits maxAllowedContentLength="1048576"/>
      </requestFiltering>
   </security>
</system.webServer>
Run Code Online (Sandbox Code Playgroud)


小智 6

它也困扰了我好几天.我修改了Web.config文件,但它没有用.原来我的项目中有两个Web.config文件,我应该修改ROOT目录中的那个,而不是其他的.希望这会有所帮助.


mhe*_*384 5

如果您有请求转到站点中的应用程序,请确保在根web.config中设置maxRequestLength.应用程序的web.config中的maxRequestLength似乎被忽略.