4
Follow Follow @aspdotnetsuresh @aspdotnetsuresh 2,600 ASP.NET,C#.NET,VB.NET,JQuery,JavaScript,Gridview,SQL Server,Ajax,SSRS, XML examples aspdotnet-suresh offers C#.net articles and tutorials,csharp dot net,asp.net articles and tutorials,VB.NET Articles,Gridview articles,code examples of asp.net 2.0 /3.5,AJAX,SQL Server Articles,examples of .net technologies HOM E ASP.NET AJAX GRIDVIEW JAVASCRIPT SQL JQUERY OOPS CONCEPTS INTERVIEW QUESTIONS TRACE MOBILE NUMBER ADVERT Restrict/Increase the size of file upload in asp.net By: Suresh Dasari Dec 14, 2010 Categories: Asp.net , Fileupload Introduction: Here I will explain how to increase or restrict the size of file upload in asp.net Description: 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 < system.web> < httpRuntime executionTimeout = " 9999" maxRequestLength= " 2097151" /> </system.web> 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="http://www.w3.org/1999/xhtml"> < head id="Head1" runat ="server"> < title> Untitled Page</title> </head> < body> < form id="form1" runat ="server"> < div> < asp:FileUpload ID="FileUpload1" runat ="server" /> < br /> < asp:Button ID="Button1" runat ="server" Text ="Button" onc lic k="Button1_Click" /> < br /> < asp:Label ID="Label1" runat ="server" Text ="Label"></asp:Label > </div> </form> </body> </html > After that write the following code in code behind protected void Button1_Clic k(object sender, EventArgs e) { if (FileUpload1.HasFile)

Restrict_Increase the Size of File Upload in ASP.net - ASP.net,C#.NET,VB

Embed Size (px)

Citation preview

Page 1: Restrict_Increase the Size of File Upload in ASP.net - ASP.net,C#.NET,VB

7/16/13 Restrict/Increase the size of file upload in asp.net - ASP.NET,C#.NET,VB.NET,JQuery,JavaScript,Gridview,SQL Server,Ajax,SSRS, XML examples

www.aspdotnet-suresh.com/2010/12/how-to-restrict-size-of-upload-file-in.html 1/4

Follow Follow @aspdotnetsuresh@aspdotnetsuresh 2,600 follow ers

ASP.NET,C#.NET,VB.NET,JQuery,JavaScript,Gridview,SQLServer,Ajax,SSRS, XML examplesaspdotnet-suresh offers C#.net articles andtutorials,csharp dot net,asp.net articles andtutorials,VB.NET Articles,Gridview articles,codeexamples of asp.net 2.0 /3.5,AJAX,SQL ServerArticles,examples of .net technologies

HOME ASP.NET AJAX GRIDVIEW JAVASCRIPT SQL JQUERY OOPS CONCEPTS INTERVIEW QUESTIONS TRACE MOBILE NUMBER ADVERTISE

Restrict/Increase the size of file upload in asp.netBy: Suresh Dasari Dec 14, 2010

Categories: Asp.net , Fileupload

Introduction:

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

Description:

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

<system.web>

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

</system.web>

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="http://www.w3.org/1999/xhtml">

<head id="Head1" runat="server">

<title>Untitled Page</title>

</head>

<body>

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

<div>

<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>

</div>

</form>

</body>

</html>

After that write the following code in code behind

protected void Button1_Click(object sender, EventArgs e)

{

if (FileUpload1.HasFile)

Page 2: Restrict_Increase the Size of File Upload in ASP.net - ASP.net,C#.NET,VB

7/16/13 Restrict/Increase the size of file upload in asp.net - ASP.NET,C#.NET,VB.NET,JQuery,JavaScript,Gridview,SQL Server,Ajax,SSRS, XML examples

www.aspdotnet-suresh.com/2010/12/how-to-restrict-size-of-upload-file-in.html 2/4

{

if (FileUpload1.PostedFile.ContentLength < 20728650)

{

try

{

Label1.Text = "File name: " +

FileUpload1.PostedFile.FileName + "<br>" +

FileUpload1.PostedFile.ContentLength + " kb<br>" +

"Content type: " +

FileUpload1.PostedFile.ContentType;

}

catch (Exception ex)

{

Label1.Text = "ERROR: " + ex.Message.ToString();

}

}

else

{

Label1.Text = "File size exceeds maximum limit 20 MB.";

}

}

}

After that write the following code in web.config

<system.web>

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

</system.web>

Demo

If you like this post then Why not Join or Follow this site

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

Get the latest Asp.net, C#.net, VB.NET, jQuery, Plugins & Code Snippets for FREE by subscribing to our

Facebook, Twitter, RSS feed, or by email.

Subscribe by RSS Subscribe by Email

Follow Follow @aspdotnetsuresh@aspdotnetsuresh 2,600 follow ers

Tweet

Page 3: Restrict_Increase the Size of File Upload in ASP.net - ASP.net,C#.NET,VB

7/16/13 Restrict/Increase the size of file upload in asp.net - ASP.NET,C#.NET,VB.NET,JQuery,JavaScript,Gridview,SQL Server,Ajax,SSRS, XML examples

www.aspdotnet-suresh.com/2010/12/how-to-restrict-size-of-upload-file-in.html 3/4

Newer Post Older Post

5 comments :

Give your Valuable Comments

Home

Subscribe to: Post Comments ( Atom )

Other Related Posts

1

2

3

4

5

duggu said...

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

March 24, 2012 at 2:17 PM

Anonymous said...

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

tnx

jasraj

June 24, 2012 at 5:06 AM

Anonymous said...

how can i upload an image with some text data

July 24, 2012 at 11:47 PM

Anonymous said...

test

September 11, 2012 at 11:02 PM

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

December 5, 2012 at 4:25 AM

Page 4: Restrict_Increase the Size of File Upload in ASP.net - ASP.net,C#.NET,VB

7/16/13 Restrict/Increase the size of file upload in asp.net - ASP.NET,C#.NET,VB.NET,JQuery,JavaScript,Gridview,SQL Server,Ajax,SSRS, XML examples

www.aspdotnet-suresh.com/2010/12/how-to-restrict-size-of-upload-file-in.html 4/4

3 tier architecture example in asp.net with C#

Asp.net insert, Edit, update, delete data in gridview

Introduction to Object Oriented Programming Concepts (OOPS) in C#.net

jQuery 360 Degrees Image Display Plugins Examples with Tutorial

Introduction to WCF - WCF tutorial | WCF Tutorial - Windows Communication Foundation | WCF Example |

WCF Sample code in asp.net 3.5 | Basic WCF Tutorial for Beginners

Interview Questions in ASP.NET,C#.NET,SQL Server,.NET Framework

how to insert images into database and how to retrieve and bind images to gridview using asp.net (or)

save and retrieve images from database using asp.net

Simple login form example in asp.net Check Username and Password availability in database

Ajax ModalPopUpExtender Example to edit the gridview row values in asp.net

Ajax Cascading Dropdownlist Sample with database using asp.net