简体   繁体   中英

Kept being directed to HTTP Error 404.13 page despite configuration set in Web.Config

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 (Eg 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:

    <configuration>
  <system.web>
    <compilation debug="true" targetFramework="4.7.2"/>
    <httpRuntime targetFramework="4.7.2" maxRequestLength="1048576" />
  </system.web>
  <system.webServer>
    <security>
      <requestFiltering>
        <requestLimits maxAllowedContentLength="1073741824" />
      </requestFiltering>
    </security>
  </system.webServer>
  <system.codedom>
    <compilers>
      <compiler language="c#;cs;csharp" extension=".cs"
        type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=2.0.1.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
        warningLevel="4" compilerOptions="/langversion:default /nowarn:1659;1699;1701"/>
      <compiler language="vb;vbs;visualbasic;vbscript" extension=".vb"
        type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.VBCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=2.0.1.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
        warningLevel="4" compilerOptions="/langversion:default /nowarn:41008 /define:_MYTYPE=\&quot;Web\&quot; /optionInfer+"/>
    </compilers>
  </system.codedom>

</configuration>

WebForm1.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="Sample_File_Upload.WebForm1" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <div>
        </div>
        <asp:FileUpload ID="FileUpload1" runat="server" />
        <p>
            &nbsp;</p>
        <p>
            <asp:Button ID="UploadFile" runat="server" OnClick="UploadFile_Click" Text="Upload File" />
        </p>
        <asp:Label ID="message" runat="server"></asp:Label>
    </form>
</body>
</html>

WebForm1.aspx.cs

using System;

namespace Sample_File_Upload
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void UploadFile_Click(object sender, EventArgs e)
        {
            if(FileUpload1.PostedFile.ContentLength > 5242880)
            {
                message.Text = "Maximum size allowed is 5MB.";
            }
            else
            {
                message.Text = "File uploaded successfully";
            }
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM