ASP.Net FileUpload Size Limit Example

Updated on 04 Jul 2012,
Published on 30 Oct 2009

The File size limit validation is required for ASP.Net FileUpload control as a security feature to protect the web server being dominated by the spammers by uploading large files that kill the server cache available for loading the web pages hosted on it. The FileUpload control does not provide any in-built controlled way for applying the limit over the file size of the file selected by the user to upload it on the web server. There is also no appropriate JavaScript function and web browser that allows you to access the file at client’s computer system to get its file size before proceeding the action of uploading the file to the server memory. For validating the ASP.Net FileUpload control to disallow the user from uploading the larger size needs two settings to control the uploading.

The web.config file of ASP.Net web application provides a HTTP runtime setting that can be applied using its httpRuntime element. This web.config element has number of attributes that allows you to control the web application page requests. The maxRequestLength attribute of httpRuntime element enables you to specify the length of request using integer type value that determines the length in kilobytes. It allows you to set the maximum value upto 2 GB to enable the large page requests. This setting also handles the large file uploading using FileUpload control by setting the limit on file size. The httpRuntime element of web.config settings is a child element of system.web element. You can declare it in the web.config file using following code:

<!-- maxRequestLength limit in kilobytes [2GB max.] -->
<!-- 2097151 kilobytes is the maximum limit for http request -->
<httpRuntime maxRequestLength="2048" />

The second step needs a server end C# code to verify the size of the uploaded file. The ASP.Net FileUpload control has a PostedFile property that provides the access to its ContentLength property for getting the size of the uploaded file. You can build the conditional code block according to the file size returned by the ContentLength property to save the file only if its size in not exceeding the limits. The ContentLength property of the FileUpload control returns the integer type value in bytes that can be compared with maximum file size limit.

Sample Code for Applying File Size Limit on FileUpload control

HTML Code
<asp:FileUpload ID="FileUpload1" runat="server" />
<asp:Button ID="Button1" 
    runat="server" 
    Text="Submit" 
    OnClick="Button1_Click" />
<br />
<br />
<asp:Label ID="Label1" runat="server"></asp:Label></div>
C# Code
protected void Button1_Click(object sender, EventArgs e)
{
    
    if (FileUpload1.HasFile)
    {
        try
        {
            // Set file size limit(in bytes) condition to disable the 
            // users to upload large files. Here 1024 bytes = 1 kilobyte.
            if (FileUpload1.PostedFile.ContentLength < 1048576)
            {
                // SaveAs method of PostedFile property used
                // to save the file at specified rooted path
                FileUpload1.PostedFile.SaveAs(Server.MapPath("~/App_Data") + System.IO.Path.DirectorySeparatorChar + FileUpload1.PostedFile.FileName);

                // success message
                Label1.Text = "File " + FileUpload1.PostedFile.FileName + " uploaded successfully.";
            }
            else
            {
                // file size limit exceeded
                Label1.Text = "File size of " + Convert.ToString(FileUpload1.PostedFile.ContentLength/1024/1024) + " MB is exceeding the uploading limit.";
            }
        }
        catch (Exception ex)
        {
            // error message
            Label1.Text = "Some problem occurred while uploading the file. Please try after some time.";
        }
    }
    else
    {
        // warning message
        Label1.Text = "Please choose a file to upload.";
    }
}

In the above C# code we have used an “if condition” to check the uploaded file size. You can use this code to implement the validation for file size limit on the file uploaded using ASP.Net FileUpload control. Please note that try to set the value of maxRequestLength setting in the web.config file approximately 1 MB greater than the ContentLength comparison value used in the C# code so that it could verify the file size and display the message accordingly.

There is one issue with FileUpload control that it throws an unhandled exception if user tries to upload the file having file size greater than the value specified for maxRequestLength setting in the web.config file. It shows the errors like:

"The connection was reset."

"The connection to the server was reset while the page was loading."

"You are not connected to the internet."

“The website is encountering problems."

If you have any ideas to handle this issue then you can send your code to us for publishing to help other developers.

Continue to the next tutorial: ASP.Net FileUpload File Type Filter using JavaScript Validation to disallow the users to upload the files having in-valid file extensions.

0 Responses to "ASP.Net FileUpload Size Limit Example"
Leave a Comment
* required
* required
* will not be published
* optional
* hint: http://www.example.com
  • Subscribe via Email