You must Sign In to post a response.

Restricting image size in file upload control using 2.0

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


Author: Shekar.Y09 Aug 2010 Member Level: Gold   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.

Author: Nisar09 Aug 2010 Member Level: Gold   Points : 2

refer this link

Author: Paritosh Mohapatra09 Aug 2010 Member Level: Gold   Points : 2


Please check the following code:


Untitled Page

ErrorMessage="File Size Cannot Exceed 40 KB" OnServerValidate="CustomValidator1_ServerValidate">

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

Author: Anil Kumar Pandey09 Aug 2010 Member Level: Diamond   Points : 2

You need to check it server side.. like

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

Author: chirag09 Aug 2010 Member Level: Silver   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

Author: pavan kumar09 Aug 2010 Member Level: Bronze   Points : 2

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

Sign In to post your comments