Error faced: HTTP Error 404.13 - Not Found/ The request filtering module is configured to deny a request that exceeds the request content length
Edit: Decided to rephrase and tidy my question.
I kept encountering this error whenever I am submitting files via FileUpload Control. I want to set the file size to 5MB, if selected files exceed 5MB, it will display a error message like (E.g. File exceeded file limit, please try again). Following are my codes.
**
WebPage1.aspx.cs
**
protected void Button1_Click(object sender, EventArgs e)
{
string gen = "Pending";
//This portion is for storing files in database
FileInfo fi = new FileInfo(FileUpload1.FileName);
byte[] documentContent = FileUpload1.FileBytes;
string name = fi.Name;
//string extn = fi.Extension;
string filextn = Path.GetExtension(FileUpload1.FileName);
//This portion is for storing files in folder
string filepath = Path.GetExtension(FileUpload1.FileName);
if (filepath.ToLower() != ".pdf" && filepath.ToLower() != ".png" && filepath.ToLower() != ".gif" && filepath.ToLower() != ".zip")
{
lblmessage.Text = "Only pdf, png and gif file are accepted";
}
else
{
//int filesize = FileUpload1.PostedFile.ContentLength;
if (FileUpload1.PostedFile.ContentLength > 5242880)
{
//lblmessage.Text = "Maximum size (5MB) exceeded";
Response.Redirect("Error.aspx");
}
}
Web.Config
From my understanding maxRequestLength is measured in KILOBYTES (KB)
<httpRuntime targetFramework="4.7.2" maxRequestLength="5000" />
And, maxAllowedContentLength is measured in BYTES
<requestFiltering> <requestLimits maxAllowedContentLength="5242880" /> </requestFiltering>
Other things that I had tried
- I configure the maxRequestLength in the configuration editor; still failed.
Credit: https://weblog.west-wind.com/posts/2016/apr/06/configuring-aspnet-and-iis-request-length-for-post-data https://hoststud.com/resources/how-to-increase-the-maximum-upload-file-size-in-iis.422/
Try with the below configurations
Web.config:
WebForm1.aspx
WebForm1.aspx.cs