Prevent 413 error request entity too large in ASP.NET [duplicate] - c#

This question already has answers here:
(413) Request Entity Too Large | uploadReadAheadSize
(15 answers)
Closed 3 years ago.
I have a multipart file upload, and when I select more than 1 file I am getting a 413 error from my server.
How can I prevent the server throwing this error and increase the maximum request size?
I tried editing a web.config file in the root of my project
<system.web>
<!-- maxRequestLength for asp.net, in KB -->
<httpRuntime maxRequestLength="10485760" ></httpRuntime>
</system.web>
<system.webServer>
<security>
<requestFiltering>
<!-- maxAllowedContentLength, for IIS, in bytes -->
<requestLimits maxAllowedContentLength="10737418240" ></requestLimits>
</requestFiltering>
</security>
</system.webServer>
but that didn't do anything

You can try to increase the file size limit in machine config of the server

Related

.Net core api returns 404 while saving large file

I have .net core api which saves file on a file server. It works fine when file sizes are small such as 10 kb , 1 Mb, 10 Mb. However when I try to save file of 100 mb, It returns :
404 - File or directory not found.
The resource you are looking for might have been removed, had its name changed, or is temporarily unavailable.
I have updated kestrel options to ignore file size:
.UseKestrel(options => {
options.Limits.MaxRequestBodySize = null;
})
File saving code:
using (var imageFile = new FileStream(fullPath, FileMode.Create))
{
imageFile.Write(bytes, 0, bytes.Length);
imageFile.Flush();
}
Web config:
<system.webServer>
<security>
<requestFiltering>
<requestLimits maxAllowedContentLength="4294967295" />
</requestFiltering>
</security>
However it still does not work(for large file size) when hosted on server .
Though it works fine when I host in my local machine using iis express.
What could be the possible reason? Or Any settings I am missing here?
please check Unexpected Not Found error with IIS. You need to increase/set maxAllowedContentLength
<system.webServer>
<security>
<requestFiltering>
<!-- This will handle requests up to 50MB -->
<requestLimits maxAllowedContentLength="52428800" />
</requestFiltering>
</security>
</system.webServer>

404 Error - Uploading file size greater than 30 MB

In our Web API, we can not upload fize size which is more than 30MB. We used to get 404 Error such as "404 - File or directory not found.
The resource you are looking for might have been removed, had its name changed, or is temporarily unavailable."
By Googling and seeing various post, I tired the below changes in my config file:
Web.Config:
<system.web>
<httpRuntime maxRequestLength="204800" timeout="7200" />
</system.web>
<requestFiltering>
<requestLimits maxAllowedContentLength="209715200" maxQueryString="2097151" maxUrl="10999"/>
</requestFiltering>
But still, I could not upload file which is more than 30MB size. But the same code is working fine to upload file which is below 30 MB.
Any thing that i had missed here?
1.) Open IIS Manager.
2.) Select the website that you want to configure.
3.) Make sure you are in Features View per the button at the bottom of the manager.
4.) Select Requests Filtering and open it by double-clicking the icon. The Request Filtering pane displays.
5.) From the Actions pane on the right hand side of the screen click Edit Feature Settings... link. The Edit Request Filtering Settings window displays.
6.) In the Request Limits section, enter the appropriate Maximum allowed content length (Bytes) and then click the OK button.
Restart IIS.
This worked for me :)
Can you change your web.config to use system.webServer and security?
<configuration>
<system.web>
<httpRuntime maxRequestLength="204800" />
</system.web>
<system.webServer>
<security>
<requestFiltering>
<requestLimits maxAllowedContentLength="209715200" />
</requestFiltering>
</security>
</system.webServer>
</configuration>
Putting in what worked for me.
I had to make two changes:
update web.config with following settings:
<system.webServer>
<security>
<requestFiltering>
<requestLimits maxAllowedContentLength="209715200" />
</requestFiltering>
</security>
</system.webServer>
But using just this was not working for me. I had to add another attribute to API Controller to make it work.
[HttpPost]
//[DisableRequestSizeLimit]
[RequestSizeLimit(70_000_000)] //Files sizes upto 70 MB are allowed
My setup was Blazor WebAssembly template with ASP Hosted option. Hope this helps somebody.

How to Upload Large files in Application Folder

In my application I'm uploading large audio files (upto 100MB). For that in my web.config file I have added:
<httpRuntime executionTimeout="90" maxRequestLength="150000" useFullyQualifiedRedirectUrl="false" minFreeThreads="8" minLocalRequestFreeThreads="4" appRequestQueueLimit="100" enableVersionHeader="true" />
With this code I'm uploading files successfully into my application folder. But when deployed in IIS and published locally, the files are not uploading and I'm getting this Error. How can i solve this issue?
In addition to the check specified in the httpRuntime node, I believe microsoft is now also checking the content length as part of the security filtering. You may be able to get past the size limits by adding the following node to your web.config
<system.webServer>
<security>
<requestFiltering>
<requestLimits maxAllowedContentLength="100000000" />
</requestFiltering>
</security>
</system.webServer>

maxAllowedContentLength override

I have the following setting in my root web.config:
<security>
<requestFiltering>
<requestLimits maxAllowedContentLength="20000000" />
</requestFiltering>
</security>
I am trying to limit the size of POST requests to my pages to 1 KB in my web application. However, I want to do it without changing the above setting. Therefore, in web.config under Views folder, I added the below lines:
<security>
<requestFiltering>
<requestLimits maxAllowedContentLength="1000"/>
</requestFiltering>
</security>
Which doesn't work, i.e my application still accepts POST requests with a large body. I tried the below settings unsuccessfully:
Using <location> tag (with allowOverride=true)
Timeouts and maxRequestLength attribute
My application still accepts requests with a large POST body (upto 20 MB - as specified in root web.config). Is there any workaround for this? Is it possible to do it programatically? I searched everywhere but couldn't find any examples.
Try this: Add under appSettings
<add key="aspnet:MaxJsonDeserializerMembers" value="1024" />

Uploading files larger than maximum request length [duplicate]

This question already has answers here:
IIS7 - The request filtering module is configured to deny a request that exceeds the request content length
(3 answers)
Closed 7 years ago.
I added a File Upload Control to my website but when I add a large file, I got this error:
The request filtering module is configured to deny a request that exceeds the request content length.
I searched and found a suggested solution was to add this code in the web config
<httpRuntime targetFramework="4.5" maxRequestLength="512000000"/>
and to edit the IIS application host Config and add this code to it
<maxAllowedContentLength="512000000"/>
I made all of these steps and it did not work. I still cannot upload a file larger than the max size.
If you are using IIS6 you need to set the following in your Web.Config (kilobytes and default is 4096 which is 4 MB):
<system.web>
<httpRuntime targetFramework="4.5" maxRequestLength="4096000" />
</system.web>
If you are using IIS7 or later (in bytes and default is 30000000 which is almost 30MB):
<system.webServer>
<security>
<requestFiltering>
<requestLimits maxAllowedContentLength="4096000000" />
</requestFiltering>
</security>
</system.webServer>
Make sure you don't add these tags if they already exist. Edit the existing ones otherwise.
Are running on dev machine? IIS Express or Cassini?
Any way thet thing is to set up your web.config.
Try this solution:
<httpRuntime
executionTimeout="90"
maxRequestLength="1024000"
useFullyQualifiedRedirectUrl="false"
minFreeThreads="8"
minLocalRequestFreeThreads="4"
appRequestQueueLimit="100"
enableVersionHeader="true"
/>
http://www.codeproject.com/Articles/10842/Setting-up-Web-config-to-allow-uploading-of-large
be sure that you add this key under <system.web> and also add a attribute "execution time" as below code:
<httpRuntime executionTimeout="10000"maxRequestLength="10240000"maxQueryStringLength="2097151"/>

Categories