Wednesday 1 January 2020

How to resolve maximum requested length exceeded error with file upload control – PART - III

Agenda :
            Dear all In this article I would like to demonstrate how to resolve maximum requested length exceeded error with file upload control using Asp.Net / Asp.Net MVC

Description :
          In previous articles, I explained on How to work with File upload control using Asp.Net – PART - I, Working with File Upload control using Asp.Net – PART - II different articles related to C#.Net,Asp.NetSQL ServerXML, and DotNetInterviewQuestions Related articles. Now Let's focus on how to resolve maximum requested length exceeded error with file upload control using Asp.Net


The root cause of “Maximum request length exceeded” :
  • If you are uploading larger files to the server then we will encounter this error
  • Whenever we are communicating with discrete systems either with services / API's if the response size is larger than usual
  • Server-side handling cases when we input the things especially with any types of files and response formats.



Workaround for “Maximum request length exceeded” :
  • The default maximum file size is 4MB. If the file size exceeds >4MB we will encounter this error.
  • The below setting is overrides the existing file size it has to be placed in web.config.For instance, to expand the upload limit to 41 MB then we have to 41*1024=41984
<system.web>
<httpRuntime targetFramework="4.5.1" maxRequestLength=“41984" executionTimeout="180" />
    </system.web>

  • In IIS7 later version has a built-in request scanning which would be capped upload file size to 30MB.To Increase this we need to add the below lines in web.config
<system.webServer>

   <security>

      <requestFiltering>

         <requestLimits maxAllowedContentLength="3000000000" />

      </requestFiltering>

   </security>

</system.webServer>



Points To Remember :

  • maxRequestLength measures in KB(Kilobytes)
  • maxAllowedContentLength measured in bytes
  • This is a security feature. Do not change this feature unless the scope of the change is fully understood. You can configure the IIS server to reject requests whose content length is greater than a specified value. If the request's content length is greater than the configured length, this error is returned.
Source Code (Design):
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="RegForm.aspx.cs" Inherits="RegForm" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <style type="text/css">
        .auto-style1 {
            width: 100%;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <div>
   
        <table class="auto-style1">
            <tr>
                <td>Enter Name</td>
                <td>
                    <asp:TextBox ID="txtName" runat="server"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td>Enter Qualification</td>
                <td>
                    <asp:TextBox ID="txtQualification" runat="server"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td>Enter CNo</td>
                <td>
                    <asp:TextBox ID="txtCNo" runat="server"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td>Upload Resume</td>
                <td>
                    <asp:FileUpload ID="fupResume" runat="server" />
                </td>
            </tr>
            <tr>
                <td>
                    <asp:Button ID="btnSubmit" runat="server" OnClick="btnSubmit_Click" Text="Submit" />
                </td>
                <td>&nbsp;</td>
            </tr>
        </table>
   
    </div>
    </form>
</body>
</html>

Source Code (Code Behind):
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

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

    }
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        if(fupResume.HasFile)
        {
            string fileName = fupResume.FileName;
            fupResume.SaveAs(Server.MapPath("~/Resumes/"+fileName));
            Response.Write("<script>alert('Please Uploaded Successfully !!!')</script>");
        }
        else
        {
            Response.Write("<script>alert('Please Choose Your Resume which is mandatory!!!')</script>");
        }
    }
}

Web.Config Code :

<?xml version="1.0"?>

<!--
  For more information on how to configure your ASP.NET application, please visit
  http://go.microsoft.com/fwlink/?LinkId=169433
  -->

<configuration>

    <system.web>
      <compilation debug="true" targetFramework="4.5.1" />
      <httpRuntime targetFramework="4.5.1" maxRequestLength="41984" executionTimeout="180" />       
    </system.web>
  <system.webServer>
   <security>
      <requestFiltering>
         <requestLimits maxAllowedContentLength="3000000000" />
      </requestFiltering>
   </security>
</system.webServer>

</configuration>


No comments :

Post a Comment