1177

I am getting the error Maximum request length exceeded when I am trying to upload a video in my site.

How do I fix this?

0

16 Answers 16

2191

If you are using IIS for hosting your application, then the default upload file size is 4MB. To increase it, please use this below section in your web.config -

<configuration>
    <system.web>
        <httpRuntime maxRequestLength="1048576" />
    </system.web>
</configuration>

For IIS7 and above, you also need to add the lines below:

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

Note:

  • maxRequestLength is measured in kilobytes
  • maxAllowedContentLength is measured in bytes

which is why the values differ in this config example. (Both are equivalent to 1 GB.)

18
  • 7
    Thank you sachin and i added some thing like <httpRuntime maxRequestLength="32768" executionTimeout="180" /> Oct 4, 2010 at 9:04
  • 43
    With both IIS 7.5 and VS RC 2012 IIS Express I had to set BOTH of these. The httpRuntime one configures ASP.NET's max length while requestLimits configures IIS's max length, stackoverflow.com/questions/6327452/… and forums.iis.net/t/1169846.aspx
    – Despertar
    Aug 6, 2012 at 8:21
  • 14
    Make sure that you're adding this setting to the main Web.config instead of the one inside the Views folder
    – Serj Sagan
    Mar 4, 2013 at 15:29
  • 3
    @SachinShanbhag Please include IMPORTANT: Both of these values must match. In this case, my max upload is 1024 megabytes. from Karls answer to make this right, then it works. Sep 3, 2013 at 15:40
  • 3
    Your calculations are in Kibibytes and Gibibyte, time to update the answer? Hehe. Apr 8, 2015 at 21:07
598

I don't think it's been mentioned here, but to get this working, I had to supply both of these values in the web.config:

In system.web

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

And in system.webServer

<security>
    <requestFiltering>
        <requestLimits maxAllowedContentLength="1073741824" />
    </requestFiltering>
</security>

IMPORTANT : Both of these values must match. In this case, my max upload is 1024 megabytes.

maxRequestLength has 1048576 KILOBYTES, and maxAllowedContentLength has 1073741824 BYTES.

I know it's obvious, but it's easy to overlook.

14
  • 5
    To any it may concern: This answer also works perfectly for IIS-Express (asp.net-webpages with WebMatrix)
    – VoidKing
    Mar 1, 2013 at 21:36
  • 5
    Yes, this is answer that worked for me instead of Sachin's answer. It works on Azure too. Mar 21, 2013 at 10:00
  • 5
    This is definitely the correct answer... both entries must be present. In case of MVC 3, it can be in the project root web.config file. Jun 3, 2013 at 22:05
  • 2
    another important thing is the 'executionTimeout' which Karl mentions Aug 20, 2013 at 14:22
  • 29
    I had to combine this with an existing line for: <httpRuntime targetFramework="4.5" maxRequestLength="1048576" executionTimeout="3600" />
    – Ken Mc
    Nov 20, 2013 at 0:53
221

It may be worth noting that you may want to limit this change to the URL you expect to be used for the upload rather then your entire site.

<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>
10
  • Nice Solution thinking about the location. But is there a location path when working with MVC (and therefore working with routes)? Aug 6, 2014 at 14:02
  • 1
    It would be a URL corresponding to the route that you have your upload action set to. In my case, I have a Documents controller with an action called Upload that I post the form to. In your case it would be whatever the url to your action would be. Aug 6, 2014 at 16:17
  • For anyone who likes this answer, see this. Jan 7, 2015 at 11:44
  • 2
    Dude you rock. Sorry, but my tiny little up vote wasn't enough. Oct 29, 2017 at 5:24
  • 1
    This will work with WebAPI for up to and including MVC v5. Once you hit ASP.NET Core with MVC Core, then there are different options you should set for Kestrel. Sep 5, 2018 at 1:03
57

And just in case someone's looking for a way to handle this exception and show a meaningful explanation to the user (something like "You're uploading a file that is too big"):

//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 (((System.Web.HttpException)httpException.InnerException).WebEventCode == System.Web.Management.WebEventCodes.RuntimeErrorPostTooLarge)
    {
        //handle the error
        Response.Write("Too big a file, dude"); //for example
    }
}

(ASP.NET 4 or later required)

4
  • 1
    maxAllowedContentLength should be higher than (maxRequestLength * 1024) for the exception generation. Jan 19, 2016 at 4:50
  • 1
    This post gave me what I needed to warn the user but HttpContext.Current.ClearError() was needed to allow the Response.Redirect() to work properly. In terms of web.config it works just with the maxRequestLength attribute of httpRuntime.
    – nrod
    Nov 28, 2018 at 17:12
  • File size validation and user-friendly message can be done via JS at the page level using an onchange event on the upload button and comparing the upload file size with the max upload limit. Mar 28, 2019 at 3:22
  • this doesn't seem to get thrown if the request is bigger than maxAllowedContentLength. In that case, IIS appears to respond before as ASP is invoked. So need to set this very large
    – Geoduck
    Mar 5, 2021 at 1:21
32

The maximum request size is, by default, 4MB (4096 KB)

This is explained here.

The above article also explains how to fix this issue :)

2
  • 4
    The link is redirecting me to the microsoft support homepage
    – Bishop
    Oct 13, 2015 at 16:17
  • 1
    Maybe something was wrong. The link redirects here
    – gofr1
    Jul 7, 2016 at 5:18
26

If you can't update configuration files but control the code that handles file uploads use HttpContext.Current.Request.GetBufferlessInputStream(true).

The true value for disableMaxRequestLength parameter tells the framework to ignore configured request limits.

For detailed description visit https://msdn.microsoft.com/en-us/library/hh195568(v=vs.110).aspx

3
  • AWESOME, so how does the top answer have > 1600 votes, and this only 5? This is terrific, because so often, it's just a single action we want this on. Further, no messing with other settings, etc. Jun 1, 2018 at 16:39
  • This is what I was looking for. Jul 5, 2018 at 18:02
  • @Nicholas Petersen as the help file describes, you cannot use this method from an .aspx page, and this is why Jul 27, 2022 at 12:15
20

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

<httpRuntime 
    maxRequestLength="1048576"
  />
19

To summarize all the answers in a single place:

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

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

Rules:

  • maxRequestLength (expressed in kb) value must match maxAllowedContentLength (expressed in bytes).
  • most of the time your system.web section may already contains an "httpRuntime". set your targetFramework to the version of your .net used.

Notes:

  • default value for maxRequestLength is 4096 (4mb). max value is 2,147,483,647
  • default value for maxAllowedContentLength is 30,000,000 (around 30mb). max value is 4,294,967,295

more info MSDN

2
  • This should be inside <configuration> </configuration> right?
    – Allie
    May 19, 2022 at 6:46
  • @S.B. affirmative
    – BernieSF
    Jun 7, 2022 at 21:19
10

maxRequestLength (length in KB) Here as ex. I took 1024 (1MB) maxAllowedContentLength (length in Bytes) should be same as your maxRequestLength (1048576 bytes = 1MB).

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

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

It bothered me for days too. I modified the Web.config file but it didn't work. It turned out that there are two Web.config file in my project, and I should modified the one in the ROOT directory, not the others. Hope this would be helpful.

0
5

If you have a request going to an application in the site, make sure you set maxRequestLength in the root web.config. The maxRequestLength in the applications's web.config appears to be ignored.

1
  • If I could upvote this more than once I would. This wasted a day of my life. If you have a virtual dir sub-application, you have to put the httpRuntime maxRequestLength="### and requestLimits maxAllowedContentLength in a web config at the root level, not at the sub-app level.
    – nickvans
    Feb 8, 2017 at 14:03
1

I was tripped up by the fact that our web.config file has multiple system.web sections: it worked when I added < httpRuntime maxRequestLength="1048576" /> to the system.web section that at the configuration level.

1

I had to edit the C:\Windows\System32\inetsrv\config\applicationHost.config file and add <requestLimits maxAllowedContentLength="1073741824" /> to the end of the...

<configuration>
    <system.webServer>
        <security>
            <requestFiltering>

section.

As per This Microsoft Support Article

1
  • web.config from your own project should override these, so I don't see the need to modify applicationHost.config.
    – bvgheluwe
    Nov 27, 2019 at 10:40
1

I was dealing with same error and after spending time solved it by adding below lines in web.config file

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

and

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

Caution: As some have pointed out, there was already an entry for <httpRuntime.. in my web.config file. I had blindly copied and pasted another httpRuntime from here and it crashed the whole site.

-3

I can add to config web uncompiled

<system.web> 
  <httpRuntime maxRequestLength="1024" executionTimeout="3600" /> 
  <compilation debug="true"/> 
</system.web> 
<security> 
  <requestFiltering> 
    <requestLimits maxAllowedContentLength="1048576"/> 
  </requestFiltering> 
</security>
1
  • 1
    The executionTimeout attribute has nothing to do with what is asked, and neither does the compilation tag. Aug 9, 2016 at 14:24

Not the answer you're looking for? Browse other questions tagged or ask your own question.