In this post, we want to discuss an error “maximum request length exceeded” of FileUploader control in ASP.Net. Before we get started, if you want to know about the uses of a function key, please go through the following article: The best use of Function Keys (F1 to F12).
Introduction
ASP.NET FileUpload control allows us to upload files to a Web Server or storage in a Web Form. The control is a part of ASP.NET controls and can be placed on a Web Form by simply dragging and dropping from Toolbox to a WebForm. The FileUpload control was introduced in ASP.NET 2.0.
With ASP.NET, accepting file uploads from users has become extremely easy. With the FileUpload control, it can be done with a small number of code lines, as you will see in the following example.
How to upload a file using the FileUploader control
1 2 3 4 5 6 7 8 |
<form id="form1" runat="server"> <asp:FileUpload id="FileUploadControl" runat="server" /> <asp:Button runat="server" id="UploadButton" text="Upload" onclick="UploadButton_Click" /> <br /><br /> <asp:Label runat="server" id="StatusLabel" text="Upload status: " /> </form> |
And here is the code-behind code required to handle the upload:
Code-behind code of FileUploader control
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
protected void UploadButton_Click(object sender, EventArgs e) { if(FileUploadControl.HasFile) { try { string filename = Path.GetFileName(FileUploadControl.FileName); FileUploadControl.SaveAs(Server.MapPath("~/") + filename); StatusLabel.Text = "Upload status: File uploaded!"; } catch(Exception ex) { StatusLabel.Text = "Upload status: The file could not be uploaded. The following error occured: " + ex.Message; } } } |
Maximum request length exceeded Problem
Uploading files via the FileUpload control gets tricky with big files. The default maximum file size is 4MB – this is done to prevent denial of service attacks in which an attacker submitted one or more huge files which overwhelmed server resources. If a user uploads a file larger than 4MB, they’ll get an error message: “Maximum request length exceeded.”
Efficient solution for the problem
1 2 3 4 5 |
<system.web> <httpRuntime executionTimeout="240" maxRequestLength="20480" /> </system.web> |
IIS7 (and later version) has a built-in request scanning which imposes an upload file cap that defaults to 30MB. To increase it, you also need to add the lines below:
1 2 3 4 5 6 7 8 9 |
<system.webServer> <security> <requestFiltering> <requestLimits maxAllowedContentLength="3000000000" /> </requestFiltering> </security> </system.webServer> |
Note: maxAllowedContentLength is measured in bytes while maxRequestLength is measured in kilobytes.
The article was published on March 1, 2020 @ 12:12 PM
Leave a Comment