1169

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?

Neeraj Kumar
  • 771
  • 2
  • 16
  • 37
Surya sasidhar
  • 29,607
  • 57
  • 139
  • 219

16 Answers16

2178

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.)

SharpC
  • 6,974
  • 4
  • 45
  • 40
Sachin Shanbhag
  • 54,530
  • 11
  • 89
  • 103
  • 7
    Thank you sachin and i added some thing like – Surya sasidhar Oct 04 '10 at 09:04
  • @Surya- The length is in kbytes. So you can set any size as you want in MaxRequestLength. My example sets the size to 1gb, yours is 32MB – Sachin Shanbhag Oct 04 '10 at 09:07
  • 42
    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, http://stackoverflow.com/questions/6327452/which-gets-priority-maxrequestlength-or-maxallowedcontentlength and http://forums.iis.net/t/1169846.aspx – Despertar Aug 06 '12 at 08: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 04 '13 at 15:29
  • i have iis 8, i did what you said, but still not exceeded, any solution for iis 8 ? – Tarek Jul 26 '13 at 13:13
  • @AmbiguousTk - As far as I know, it should be same settings for IIS8 too. what is the limit you are trying to set? Seems the max upload limit you can set is upto 2GB. – Sachin Shanbhag Jul 26 '13 at 14:17
  • @SachinShanbhag i set it to 1000000000 byte which means 953 mb, and i restarted the web site, yet the error is showing up... – Tarek Jul 26 '13 at 14:22
  • 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. – Don Thomas Boyle Sep 03 '13 at 15:40
  • 3
    Your calculations are in Kibibytes and Gibibyte, time to update the answer? Hehe. – Rosdi Kasim Apr 08 '15 at 21:07
  • @SachinShanbhag any idea how to implement this on java? http://stackoverflow.com/questions/31914474/system-web-httpexception-maximum-request-length-exceeded – Ondrej Tokar Aug 10 '15 at 11:37
  • Be careful that in your web config file this node may be already have so you just add additional attribute only then.Just copy and past wont work since it already define the attribute then trow a error. – Prageeth godage Jan 08 '16 at 04:43
  • I have multiple Web.config files in my project - do they all need these entries? – B. Clay Shannon-B. Crow Raven Feb 04 '16 at 21:14
  • 1
    @B.ClayShannon add it to the root web.config file. – yogihosting May 04 '16 at 11:25
  • @tarek try adding executionTimeout="3600" to httpRuntime and see if this solves your problem. – yogihosting May 04 '16 at 11:26
  • Shoud I increase uploadReadAheadSize as well? – mtkachenko May 04 '16 at 15:36
  • In my case the Root web.config file was located under \Windows\Microsoft.NET\Framework\v4.0.30319\Config\ – 0014 Oct 12 '16 at 15:56
  • When I do this I receive an error saying my config file couldn't be read. HTTP Error 500.19 sites the line I add as being the source of the error. Is there something else I need to do? – Brad Apr 06 '17 at 19:39
  • Does this apply to Azure App Service? – hiFI Sep 24 '20 at 04:57
596

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.

Amirhossein Mehrvarzi
  • 18,024
  • 7
  • 45
  • 70
Karl
  • 6,793
  • 3
  • 23
  • 21
218

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>
Nick Albrecht
  • 16,607
  • 10
  • 66
  • 101
54

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)

Community
  • 1
  • 1
Serge Shultz
  • 5,888
  • 3
  • 27
  • 17
  • 1
    maxAllowedContentLength should be higher than (maxRequestLength * 1024) for the exception generation. – Der_Meister Jan 19 '16 at 04: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 '18 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. – Alfred Wallace Mar 28 '19 at 03: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 – Garr Godfrey Mar 05 '21 at 01: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 :)

Arsen Khachaturyan
  • 7,904
  • 4
  • 42
  • 42
Dave
  • 6,905
  • 2
  • 32
  • 35
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

Sergey Tarasov
  • 858
  • 11
  • 18
20

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

<httpRuntime 
    maxRequestLength="1048576"
  />
bkaid
  • 51,465
  • 22
  • 112
  • 128
ema
  • 5,668
  • 1
  • 25
  • 31
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

BernieSF
  • 1,722
  • 1
  • 28
  • 42
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>
CountZero
  • 6,171
  • 3
  • 46
  • 59
UniCoder
  • 3,005
  • 27
  • 26
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.

NiaoBlush
  • 61
  • 1
  • 1
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.

mhenry1384
  • 7,538
  • 5
  • 55
  • 74
  • 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 08 '17 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.

Graham Laight
  • 4,700
  • 3
  • 29
  • 28
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

HyperActive
  • 1,129
  • 12
  • 12
  • web.config from your own project should override these, so I don't see the need to modify `applicationHost.config`. – bvgheluwe Nov 27 '19 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>
Nida Akram
  • 332
  • 2
  • 9
  • 24
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.

Soundar Rajan
  • 327
  • 2
  • 11
-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>
Paul Roub
  • 36,322
  • 27
  • 84
  • 93