You think ASP Net file upload size limit is what you think

Posted by ja_blackburn on Wed, 02 Feb 2022 06:03:46 +0100

We think the file size limit

We all know ASP Net provides us with the file upload server control FileUpload. By default, the maximum file that can be uploaded is 4M. If you want to change the size limit of the file that can be uploaded, we can Add the maxRequestLength attribute to the httpRuntime element in config to set the size. At the same time, in order to support large file upload timeout, you can add the executionTimeout attribute to set the timeout time. There are many such examples on the Internet, but is this the case?

<httpRuntime maxRequestLength="" executionTimeout=""/>

testing environment

IIS 7.5,.NET 3.5 sp1

Test page UploadFile aspx

This code is by Java Architect must see network-Structure Sorting
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="UploadFile.aspx.cs" Inherits="UploadFile" %>

 

<!DOCTYPE html>

 

<html xmlns="http://www.w3.org/1999/xhtml">

<head runat="server">

<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>

    <title></title>

</head>

<body>

    <form id="form1" runat="server">

    <div>

        <asp:FileUpload ID="FileUpload1" runat="server" />

        <asp:Button ID="Button1" runat="server" Text="Button" />

    </div>

    </form>

</body>

</html>

Test page UploadFile aspx. cs

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;

 

public partial class UploadFile : System.Web.UI.Page

{

    protected void Page_Load(object sender, EventArgs e)

    {

        if (FileUpload1.FileContent != null)

        {

            FileUpload1.SaveAs(Server.MapPath("/Files/"+FileUpload1.FileName));

        }

    }

 

}

Actual test

  1. Upload files less than 4M

Select 2.7M pdf file to upload

Upload prompt successful

      2. Upload files larger than 4M

Choose to upload 4.3M pdf file

Post upload yellow page

       3. Modify web Config file to set the upload file size limit

Modify web Config, increase the size limit of uploadable files and the execution time limit

This code is by Java Architect must see network-Structure Sorting
<httpRuntime maxRequestLength="2048000" executionTimeout="600"/>

Re select the pdf file of 4.3M and upload it again

Upload successful

      4. Upload files larger than 30M

The actual environment that encountered this problem is that we used a third-party upload file component, called the third-party ActiveX control through js to upload files and modify the web When uploading a file larger than 30M after config, it is reported

The following error

Looking at the windows system log, we can see the following log errors and abnormal information. We can guess that the request is too long, but we have set the maximum request length, which is far greater than 30M. quiet

Think about it. It may be restricted by IIS. Query the relevant IIS data and find that it is.

Exception message: Maximum request length exceeded.

   stay System.Web.HttpRequest.GetEntireRawContent()

   stay System.Web.HttpRequest.GetMultipartContent()

   stay System.Web.HttpRequest.FillInFormCollection()

   stay System.Web.HttpRequest.EnsureForm()

   stay System.Web.HttpRequest.get_HasForm()

   stay System.Web.UI.Page.GetCollectionBasedOnMethod(Boolean dontReturnNull)

   stay System.Web.UI.Page.DeterminePostBackMode()

   stay System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)

         5. Modify the configuration of IIS

Select your own site and double-click request filter

Select edit function limits on the right

In the pop-up page, we can see that the default request limit is 30M

The size of the modification request is limited to an appropriate value, and the site can be restarted after saving.

summary

      1. Asp.NET as Microsoft's Web service framework, it defines the size limit and execution time limit of web requests. At the same time, it provides a basic framework for uploading files and provides us with UploadFile server control

The file data is finally transmitted to the server through Http. Naturally, it is also limited by the request size and execution time, but the general request can not reach this limit, which is often touched when uploading files

This threshold.

      2. IIS hosts ASP.NET on windows platform Net legal server, according to normal logic, it can also uniformly set the size limit of requests sent to itself. At the same time, IIS will only target specific types of documents

Route requests to ASP Net. Requests such as js, css and pictures are not subject to ASP Net, which also shows that it is necessary to add control in IIS.

       3. The length check of the request can only be routed to ASP after passing the restriction of IIS Net, only through ASP Net can be used for subsequent pipeline processing steps normally.