0

面临的错误:HTTP 错误 404.13 - 未找到/请求过滤模块配置为拒绝超过请求内容长度的请求

编辑:决定改写和整理我的问题。

每当我通过 FileUpload Control 提交文件时,我都会遇到此错误。我想将文件大小设置为 5MB,如果选择的文件超过 5MB,它会显示错误消息(例如文件超出文件限制,请重试)。以下是我的代码。

**

网页1.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");
            }
        }
        

网络配置

  • 据我了解 maxRequestLength 以千字节(KB)为单位

      <httpRuntime targetFramework="4.7.2" maxRequestLength="5000"  />
    
  • 并且,maxAllowedContentLength 以 BYTES 为单位测量

        <requestFiltering>
      <requestLimits maxAllowedContentLength="5242880" />
    </requestFiltering>
    

我尝试过的其他事情

  • 我在配置编辑器中配置了 maxRequestLength;仍然失败。

信用:https ://weblog.west-wind.com/posts/2016/apr/06/configuring-aspnet-and-iis-request-length-for-post-data https://hoststud.com/resources/how -to-increas-the-maximum-upload-file-size-in-iis.422/

4

1 回答 1

0

尝试以下配置

网络配置:

    <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";
            }
        }
    }
}
于 2020-07-28T16:19:41.060 回答