Forums » .NET » ASP.NET »

Restricting image size in file upload control using 2.0

Posted Date: 09 Aug 2010      Posted By:: pavan kumar     Member Level: Bronze  Member Rank: 0     Points: 1   Responses: 6

Dear Team,

I have an file upload control in my applcation for uploading some logos.
I want restrict that logo size to 40kb.
can you help me how to restrict that image size using 2.0 c#.

Thanks & Regards


#535862    Author: Shekar.Y      Member Level: Gold      Member Rank: 0     Date: 09/Aug/2010   Rating: 2 out of 52 out of 5     Points: 2

You can handle the file size from server side.i.e you can validate the upload file size on the server side using the ContentLength of the file.

FileUploadConrol1.PostedFile.ContentLength will give you the length.

Check the following link for example.


#535865    Author: Nisar      Member Level: Gold      Member Rank: 0     Date: 09/Aug/2010   Rating: 2 out of 52 out of 5     Points: 2

refer this link

Nisar Ahmad
DotNetSpider MVM, Badge of Appreciation Award – 2010
Greatness Can't be achieved by doing big things, If you really want to be great, Do small things in Great way.

#535870    Author: Paritosh Mohapatra      Member Level: Gold      Member Rank: 4     Date: 09/Aug/2010   Rating: 2 out of 52 out of 5     Points: 2


Please check the following code:


<html xmlns="">
<head runat="server">
<title>Untitled Page</title>
<form id="form1" runat="server">
<asp:FileUpload ID="FileUpload1" runat="server" />
<asp:CustomValidator ID="CustomValidator1" runat="server" ControlToValidate="FileUpload1"
ErrorMessage="File Size Cannot Exceed 40 KB" OnServerValidate="CustomValidator1_ServerValidate"></asp:CustomValidator>
<br />
<br />
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" />

C# Code Behind

protected void Button1_Click(object sender, EventArgs e)
if (Page.IsValid)
FileUpload1.SaveAs(Server.MapPath("~/" + FileUpload1.FileName));

protected void CustomValidator1_ServerValidate(object source, ServerValidateEventArgs args)
if (FileUpload1.PostedFile.ContentLength >= 40960)
args.IsValid = false;
args.IsValid = true;

Thanks & Regards
Paritosh Mohapatra

Thanks & Regards
Paritosh Mohapatra
Microsoft MVP (ASP.Net/IIS)
DotNetSpider MVM

#535905    Author: Anil Kumar Pandey      Member Level: Diamond      Member Rank: 1     Date: 09/Aug/2010   Rating: 2 out of 52 out of 5     Points: 2

You need to check it server side.. like

if (FileUpload1.PostedFile.ContentLength >= 40000)
//Prompt for size exceed...

Thanks & Regards
Anil Kumar Pandey
Microsoft MVP, DNS MVM

#536076    Author: chirag      Member Level: Silver      Member Rank: 146     Date: 09/Aug/2010   Rating: 2 out of 52 out of 5     Points: 2

//below vl check the filesize

FileUpload1.PostedFile.ContentLength >= 20000(or any lengh u want)

it will upload file if it is greater than 20000 bytes

Chirag - 48
Enjoy With Errors

#536234    Author: pavan kumar      Member Level: Bronze      Member Rank: 0     Date: 09/Aug/2010   Rating: 2 out of 52 out of 5     Points: 2

Thank you so much friends for u r support.
I think the code will helps me a lot.

Post Reply     

 This thread is locked for new responses. Please post your comments and questions as a separate thread.
If required, refer to the URL of this page in your new post.

  • Return to Discussion Forum
  • Start new thread

  • Subscribe to Subscribers
    Active Members
    TodayLast 7 Daysmore...

    Awards & Gifts
    Talk to Webmaster Tony John

    Online Members

    sridhar thota
    Copyright © SpiderWorks Technologies Pvt Ltd., Kochi, India