aspdotnet-suresh offers articles and tutorials,csharp dot net, articles and tutorials,VB.NET Articles,Gridview articles,code examples of 2.0 /3.5,AJAX,SQL Server Articles,examples of .net technologies

Restrict/Increase the size of file upload in

Dec 14, 2010

Here I will explain how to increase or restrict the size of file upload in


I have one page that contains one file upload control to accept files from user and saving it in one folder. I have written code to upload file and saving it to folder it’s working fine after completion of my application my friend has tested my application like he uploaded large size file nearly 10 MB file at that time it’s shown the error page like “the page cannot displayed”. 

Again I have search in net I found that file upload control allows maximum file size is 4MB for that reason if we upload file size larger than 4MB we will get error page like “the page cannot displayed” or “Maximum request length exceeded”.

After that I tried to increase the size of uploaded file by setting some properties in web.config 
file like this 

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

Here httpRuntime means

Configures ASP.NET HTTP runtime settings. This section can be declared at the machine, site, application, and subdirectory levels.

executionTimeout means

Indicates the maximum number of seconds that a request is allowed to execute before being automatically shut down by ASP.NET.

maxRequestLength means

Indicates the maximum file upload size supported by ASP.NET. This limit can be used to prevent denial of service attacks caused by users posting large files to the server. The size specified is in kilobytes. The default is 4096 KB (4 MB).

After that write the following code in aspx page

<html xmlns="">
<head id="Head1" runat="server">
<title>Untitled Page</title>
<form id="form1" runat="server">
<asp:FileUpload ID="FileUpload1" runat="server" />
<br />
<asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />
<br />
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
After that write the following code in code behind

protected void Button1_Click(object sender, EventArgs e)
if (FileUpload1.HasFile)
if (FileUpload1.PostedFile.ContentLength < 20728650)
Label1.Text = "File name: " +
FileUpload1.PostedFile.FileName + "<br>" +
FileUpload1.PostedFile.ContentLength + " kb<br>" +
"Content type: " +
catch (Exception ex)
Label1.Text = "ERROR: " + ex.Message.ToString();
Label1.Text = "File size exceeds maximum limit 20 MB.";

After that write the following code in web.config

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

If you enjoyed this post, please support the blog below. It's FREE!

Get the latest,, VB.NET, jQuery, Plugins & Code Snippets for FREE by subscribing to our Facebook, Twitter, RSS feed, or by email.

subscribe by rss Subscribe by RSS subscribe by email Subscribe by Email


duggu said...

can u pls tell me about sharing sql database over LAN...

Anonymous said...

how we can upload large file like 100-200 mb.


Anonymous said...

how can i upload an image with some text data

Anonymous said...


rupesh said...

Hi Suresh,

I had used ajax fileupload control to upload excel file in which i don't know the path but i want to read data in the file and i don't now the sheet name and file name

Please reply for the post

Anonymous said...

how could we do the same in vb????


Hi Suresh,

How we can upload large files like 200mb to 500mb in asp 4.0 and IIS 7.5

Anonymous said...


Thank You SIR, this is very useful.

Give your Valuable Comments

Other Related Posts

© 2015 All Rights Reserved.
The content is copyrighted to Suresh Dasari and may not be reproduced on other websites without permission from the owner.