Maximum request length exceeded.
Asked Answered
B

16

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?

Bautram answered 4/10, 2010 at 8:48 Comment(0)
H
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.)

Hilde answered 4/10, 2010 at 8:52 Comment(18)
Thank you sachin and i added some thing like <httpRuntime maxRequestLength="32768" executionTimeout="180" />Bautram
@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 32MBHilde
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, #6327952 and forums.iis.net/t/1169846.aspxEmpery
Make sure that you're adding this setting to the main Web.config instead of the one inside the Views folderIolanthe
i have iis 8, i did what you said, but still not exceeded, any solution for iis 8 ?Islaen
@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.Hilde
@SachinShanbhag i set it to 1000000000 byte which means 953 mb, and i restarted the web site, yet the error is showing up...Islaen
@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.Dayan
Your calculations are in Kibibytes and Gibibyte, time to update the answer? Hehe.Herringbone
@SachinShanbhag any idea how to implement this on java? #31914974Garges
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.Maynardmayne
I have multiple Web.config files in my project - do they all need these entries?Glottology
@B.ClayShannon add it to the root web.config file.Opia
@tarek try adding executionTimeout="3600" to httpRuntime and see if this solves your problem.Opia
Shoud I increase uploadReadAheadSize as well?Nowell
In my case the Root web.config file was located under \Windows\Microsoft.NET\Framework\v4.0.30319\Config\Dissimilitude
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?Perdue
Does this apply to Azure App Service?Philbert
I
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.

Inverse answered 19/9, 2012 at 13:45 Comment(14)
To any it may concern: This answer also works perfectly for IIS-Express (asp.net-webpages with WebMatrix)Byzantine
Yes, this is answer that worked for me instead of Sachin's answer. It works on Azure too.Astarte
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.Confirmatory
another important thing is the 'executionTimeout' which Karl mentionsWould
I had to combine this with an existing line for: <httpRuntime targetFramework="4.5" maxRequestLength="1048576" executionTimeout="3600" />Composition
To all the people who are new to ASP.NET there is a web.config file and a web.Debug.config file. The edits need to go into the Web.Config file, not the web.Debug.config file.Kazak
<httpRuntime maxRequestLength="1048576" executionTimeout="3600" /> This cause error in iis express 10 and targetFramework="4.5.2" But when combine with <httpRuntime targetFramework="4.5.2" maxRequestLength="1048576" executionTimeout="3600" /> Works correctlyDesireah
I have multiple Web.config files in my project - do they all need to have these entries added?Glottology
I guess with with more the upload size, the timeout should be adjusted so that the request doesn't terminate when the upload is in progress.Unrig
@B.ClayShannon add this in the root web.config file.Opia
executionTimeout="3600" is that means 1 hour?Northumbrian
Thank you. setting both values and matching both is very important.Jabiru
I don't get it: 10 Bytes is 1 KB. How do the values in this answer correspond?Garnishee
@Garnishee 1 KB is exactly 1024 bytes. The metric, decimal-based prefixes work differently in binary, which is based on powers of 2, but they do follow decimals to some extent. ("kilo" suggests one thousand, not ten.)Olivenite
R
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>
Romanfleuve answered 6/5, 2013 at 16:57 Comment(10)
Nice Solution thinking about the location. But is there a location path when working with MVC (and therefore working with routes)?Mahmud
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.Romanfleuve
For anyone who likes this answer, see this.Spurry
I have not tested it with WebAPI but I have no reason to think it wouldn't work with itRomanfleuve
Dude you rock. Sorry, but my tiny little up vote wasn't enough.Smalltime
Worked for me with WebAPI, This is the best answer by farPersons
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.Romanfleuve
What is the parent node for location?Lenna
In my example above, the parent node is the domain. So it might be http://www.example.com/Documents/UploadRomanfleuve
Thanks for your help. I have the same values in my config, but still getting the error: <security> <requestFiltering> <requestLimits maxAllowedContentLength="‭52428800‬" /> </requestFiltering> </security>Kidney
A
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)

Arabella answered 23/5, 2015 at 19:58 Comment(4)
maxAllowedContentLength should be higher than (maxRequestLength * 1024) for the exception generation.Vortical
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.Sextet
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.Chopine
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 largeRodas
E
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 :)

Electroform answered 4/10, 2010 at 8:51 Comment(2)
The link is redirecting me to the microsoft support homepageKurtiskurtosis
Maybe something was wrong. The link redirects hereSvetlanasvoboda
S
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

Sills answered 10/8, 2017 at 7:13 Comment(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.Smalltime
This is what I was looking for.Penman
@Nicholas Petersen as the help file describes, you cannot use this method from an .aspx page, and this is whyCyan
E
20

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

<httpRuntime 
    maxRequestLength="1048576"
  />
Ejection answered 4/10, 2010 at 8:52 Comment(0)
S
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

Synonymous answered 21/3, 2018 at 17:46 Comment(2)
This should be inside <configuration> </configuration> right?Sebastiansebastiano
@S.B. affirmativeSynonymous
F
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>
Floorwalker answered 4/12, 2015 at 9:7 Comment(0)
W
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.

Warship answered 31/12, 2015 at 3:13 Comment(0)
C
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.

Conde answered 19/10, 2015 at 23:15 Comment(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.Euphrosyne
R
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.

Reggiereggis answered 18/4, 2016 at 14:52 Comment(0)
E
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

Efren answered 30/1, 2017 at 0:48 Comment(1)
web.config from your own project should override these, so I don't see the need to modify applicationHost.config.Isomerism
S
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>
Selfoperating answered 23/11, 2020 at 17:16 Comment(0)
H
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.

Humility answered 11/3, 2023 at 7:54 Comment(0)
M
-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>
Marcy answered 20/1, 2016 at 13:52 Comment(1)
The executionTimeout attribute has nothing to do with what is asked, and neither does the compilation tag.Pentecostal

© 2022 - 2024 — McMap. All rights reserved.