45

I have a file upload control on my webpage. The maximum request length is set to 8 MB (maxRequestLength = 8192). I also have server validation that throws an error if the file is more than 4MB. The reason that its 8MB in the config is the leverage that's given to the user and also so that the application can be tested.

If I upload a file that's 9MB, I get thrown an exception Maximum request length exceeded., which is fine and working as expected. But when I try to upload a file that's 1GB, it shows me a HTTP 404 - File not found. Can someone please explain why this is happening and how can I get it to throw me a maxRequestLength exception?

I'm using IIS6.

Amirhossein Mehrvarzi
  • 18,024
  • 7
  • 45
  • 70
Divi
  • 7,621
  • 13
  • 47
  • 63
  • 1
    Anyone have a solution for this? When I upload a file of 50mb, I get a 404 result, even though my maxRequestLength is set to "2097152". I have a HTTP POST flash control on my site which uploads close to 2GB with no issue, but this issue is driving me crazy! – pearcewg Jun 22 '11 at 20:32

9 Answers9

73

I experienced this condition today (HTTP 404 on large file upload with IIS 7) but I thought I had made all the correct configuration settings. I wanted to upload files up to 300MB so I made the following web.config settings in a sub-folder of the application:

<configuration>
    <system.web>
        <httpRuntime maxRequestLength="307200" />
    </system.web>
    <system.webServer>
        <security>
            <requestFiltering>
                <requestLimits maxAllowedContentLength="314572800" />
            </requestFiltering>
        </security>
    </system.webServer>
</configuration>

This configuration worked in test but when I copied the updated files including the web.config to the production server, I received the HTTP 404 error on uploading a 90MB file. Smaller files under the application-wide limit of 30MB were working fine, so I knew it was a request size problem of some sort.

I figured there was a chance IIS had cached some application settings and just hadn't updated them, so I recycled the Application Pool, after which everything worked as expected.

pseudocoder
  • 4,314
  • 2
  • 25
  • 40
  • 3
    +1 : Should be the CORRECT ANSWER for me. I first met a 500 error, so I add `maxRequestLength` but increasing file size I met error 404 and `maxAllowedContentLength` was the solution. Thanks ! – JPBlanc Apr 17 '14 at 09:06
  • Which subfolder did you make the web.config in? – Maksim Pavlov Sep 19 '17 at 08:46
  • The same subfolder the upload page is in. By default, settings in a web.config apply to all pages in that folder and its subfolders. You can also use a element to specify particular files. – pseudocoder Sep 29 '17 at 16:05
  • 1
    I missed this the first time, but for anyone reading, don't forget that maxRequestLength is in KB while maxAllowedContentLength is in bytes! – Jason Rae Oct 24 '18 at 19:19
  • Is there any harm in just making `maxAllowedContentLength` arbitrarily large? `maxAllowedContentLength` triggers before `maxRequestLength` always, which makes handling the exception tricky. I can't think of a need for two checks, if they truly do the same thing. – Sinjai Oct 30 '18 at 21:46
  • @Sinjai good question, but outside the context of this Q&A. I'd suggest creating a new question about this so it can be given a proper discussion and answers. – pseudocoder Dec 18 '18 at 21:46
14

I feel none of the answers here explain why you get a 404, they just tell you the usual stuff of how to fix the problem.

The 404 is not due to misconfiguration, it is intentional and documented behaviour:

When request filtering blocks an HTTP request because an HTTP request exceeds the request limits, IIS 7 will return an HTTP 404 error to the client and log one of the following HTTP statuses with a unique substatus that identifies the reason that the request was denied:

HTTP Substatus    Description

404.13            Content Length Too Large
404.14            URL Too Long
404.15            Query String Too Long

These substatuses allow Web administrators to analyze their IIS logs and identify potential threats.

In addition, when an HTTP request exceeds the header limits that are defined in the in the <headerLimits> element, IIS 7 will return an HTTP 404 error to the client with the following substatus:

HTTP Substatus    Description

404.10            Request Header Too Long
user247702
  • 23,641
  • 15
  • 110
  • 157
  • Informative, but still doesn't really explain why it's a potentially misleading 404 Not Found rather than any other 4xx code (400 och 413). Possibly it is assumed to potentially be some sort of attack and the best way to avert it is to pretend the file isn't there at all. – Oskar Berggren Jan 28 '19 at 18:23
11

This is a bit of an old thread, but I thought I should add my experiences with this.

I faced the same problem with large file uploads and the web api. A 404.13 is thrown before it gets to a controller at all, so I had to find out where to jump in and handle this case.

My solution was the following web.config entries:

I handle the 404.13 by redirecting it to a mvc controller (it could be a webforms page just the same), and regular 404 errors hit my 404 route. it's critical that the responseMode="redirect" for the 404.13

<httpErrors errorMode="Custom">
  <remove statusCode="404" subStatusCode="-1" />            
  <error statusCode="404" subStatusCode="13" path="/errors/filesize" responseMode="Redirect" />
  <error statusCode="404" path="/errors/notfound" responseMode="ExecuteURL" />      
</httpErrors>

Then, in my Errors controller, I have the following:

public ActionResult FileSize()
{
    Response.StatusCode = 500;
    Response.StatusDescription = "Maximum file size exceeded.";
    Response.End();
    return null;
}

Again, this could be a regular webforms page.

ScottE
  • 21,530
  • 18
  • 94
  • 131
  • 1
    The code specifically handles subStatus 13 but generically handles all other substatuses. The subsctatus -1 (default) removes all the handlers for that status. I thought the code example did not make this clear but +1 for mentioning that the .13 has to have a redirect rather than an ExecuteURL. – David Bridge Feb 03 '15 at 13:28
  • This answer saved my life. It seems to be the only way to change the behavior of ``, which always triggers before `` in IIS 7+. – Sinjai Oct 30 '18 at 20:06
3

To my knowledge, there is no way to gracefully handle exceeding IIS's "maxRequestLength" setting. It can't even display a custom error page (since there is no corresponding HTTP code to respond to). The only way around this is to set maxRequestLength to some absurdly high number of kbytes, for example 51200 (50MB), and then check the ContentLength after the file has been uploaded (assuming the request didn't time out before 90 seconds). At that point, I can validate if the file <=5MB and display a friendly error.

You can also try this link.

You could also try something like this:

private void application_EndRequest(object sender, EventArgs e)
{
    HttpRequest request = HttpContext.Current.Request;
    HttpResponse response = HttpContext.Current.Response;

    if ((request.HttpMethod == "POST") &&
        (response.StatusCode == 404 && response.SubStatusCode == 13))
    {
        // Clear the response header but do not clear errors and transfer back to requesting page to handle error
        response.ClearHeaders();
        HttpContext.Current.Server.Transfer(request.AppRelativeCurrentExecutionFilePath);
    }
}
Scott
  • 13,735
  • 20
  • 94
  • 152
1

I have found that this problem can also be caused on IIS7 (and presumably IIS6) when the URLScan tool is installed and running on the site.

When uploading the file to a website i was receiving the message "File or directory not found. The resource you are looking for might have been removed, had its name changed, or is temporarily unavailable."

If the problem is being caused by URLScan then if you try to upload the large file to the site whilst browsing the site on the hosting server itself, you will be served a full asp.net error message instead of a 404 that mentions URLScan. You can also check if URLScan is running on you site in IIS7 by viewing the ISAPI Filters for the website in IIS, URLScan will be listed if it is used.

This can be fixed by altering the ini file for URLScan is located at "%WINDIR%\System32\Inetsrv\URLscan" and changing the MaxAllowedContentLength. The MaxAllowedContentLength is in bytes.

This may require a IIS restart to take effect, though it did not when i tried it myself with IIS7.

http://www.iis.net/learn/extensions/working-with-urlscan/urlscan-overview

http://www.iis.net/learn/extensions/working-with-urlscan/common-urlscan-scenarios

wsys177
  • 11
  • 1
0

You could configure the default error page in IIS itself.

0

The request limit is a setting is IIS. Open the Request Filtering section of your site in IIS and select Edit Request Settings. For me it was that simple.

A more detailed How To from Microsoft.

https://learn.microsoft.com/en-us/iis/configuration/system.webserver/security/requestfiltering/#how-to-edit-the-request-filtering-feature-settings-and-request-limits

DarrenY
  • 21
  • 1
-1

I just met the same problem, i made the similar operation like pseudocoder's answer but have different( i think maybe is not the cache) :

  1. edit your Web.config --> maxRequestLength

    <system.web>
    <httpRuntime maxRequestLength="1073741824" executionTimeout="3600" />
    </system.web>
    
  2. edit this:

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

just like this,and try it.

willxiang
  • 291
  • 3
  • 6
  • 1
    `maxRequestLength` and `maxAllowedContentLength` use different units, one is set in bytes and the other in kilobytes so you can't have the same number on both. Check google to see which is which – Boris Dec 09 '15 at 10:39
-10

The problem with the 1GB uploads is more browser related. I have had heaps of trouble with it and tried a lot of solutions but really the question to ask here is what are the chances of this happening in the real world for your business needs and maybe it should be recorded as a known issue in the business rules or non functional requirements document.

DarK
  • 221
  • 1
  • 3
  • 16
  • 6
    This response is not helpful. It hints at the information "is more browser related" but provides no information that can be used to resolve the problem – lostinplace Dec 07 '12 at 23:39
  • The problem is IIS-related, not browser-related. See @pseudocoder's answer, which sets the `maxAllowedContentLength` in addition to `maxRequestLength`. – Sphinxxx Feb 27 '15 at 01:07
  • DarK, if you delete this answer you'll get your points back ... ! – noelicus Sep 20 '17 at 10:35