Guanxi
Guanxi

Reputation: 3131

What all places to verify file upload size limit?

I have my ASP.NET WebAPIs hosted on IIS 7.5+ which has default Max allowed content length set to 30000000 Bytes.

When I am opening Request Filtering feature of my webAPI on IIS and check the entry there, it says 30000000 Bytes, but when I am doing upload for files larger than 4MB to the webAPI it is throwing exception as below.

Message "Maximum request length exceeded."

Stack Trace:

at System.Web.HttpBufferlessInputStream.ValidateRequestEntityLength() at System.Web.HttpBufferlessInputStream.GetPreloadedContent(Byte[] buffer, Int32& offset, Int32& count) at System.Web.HttpBufferlessInputStream.BeginRead(Byte[] buffer, Int32 offset, Int32 count, AsyncCallback callback, Object state) at System.IO.Stream.b__d(Stream stream, ReadWriteParameters args, AsyncCallback callback, Object state) at System.Threading.Tasks.TaskFactory`1.FromAsyncTrim[TInstance,TArgs](TInstance thisRef, TArgs args, Func`5 beginMethod, Func`3 endMethod) at System.IO.Stream.BeginEndReadAsync(Byte[] buffer, Int32 offset, Int32 count) at System.IO.Stream.ReadAsync(Byte[] buffer, Int32 offset, Int32 count, CancellationToken cancellationToken) at System.Web.Http.WebHost.SeekableBufferedRequestStream.d__0.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult() at System.Net.Http.HttpContentMultipartExtensions.d__8.MoveNext()

Files smaller than 4MB are getting uploaded fine.

Any idea what places I should be looking for to see where this limit is set?

What I have Tried:

I have checked Server's Web.config, machine.config as well as all the application's web.config, but don't see any entry for size limit anywhere.

I have also tried setting up the entries in webAPI's web.config - maxRequestLength and maxAllowedContentLength files as mentioned in various other questions that ask for increasing the size limit for file upload. On doing that Request Filtering Feature in IIS was showing me new value but I was still getting the same exception for files larger than 4MB.

Upvotes: 4

Views: 1351

Answers (1)

Reza Aghaei
Reza Aghaei

Reputation: 125197

As stated here you should set both maxRequestLength and maxAllowedContentLength.

You can set maxRequestLength in web.config this way:

<system.web>
    <httpRuntime maxRequestLength="size in kilo bytes" />
</system.web>

This setting specifies the limit for the input stream buffering threshold, in KB. This limit can be used to prevent denial of service attacks that are caused, for example, by users posting large files to the server.

The default is 4096 (4 MB).

Also you can set maxAllowedContentLength in web.config this way:

<system.webServer>
    <security>
        <requestFiltering>
            <requestLimits maxAllowedContentLength="size in bytes" />
        </requestFiltering>
    </security>
</system.webServer>

Specifies the maximum length of content in a request, in bytes.

The default value is 30000000.

Also make sure you are changing the config in correct file. For more information about asp.net configuration file, you can see ASP.NET Configuration File Hierarchy and Inheritance

Upvotes: 3

Related Questions