Forums » .NET » ASP.NET »

Restricting image size in file upload control using apt.net 2.0


Posted Date: 09 Aug 2010      Posted By:: pavan kumar     Member Level: Bronze    Member Rank: 4886     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 asp.net 2.0 c#.

Thanks & Regards




Responses

#535862    Author: Shekar.Y      Member Level: Gold      Member Rank: 105     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.
http://programming.top54u.com/post/ASP-Net-FileUpload-Size-Limit-Example.aspx

Thanks
Shekar.Y


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

refer this link
http://forums.asp.net/t/1086821.aspx

_______________________________
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: 9     Date: 09/Aug/2010   Rating: 2 out of 52 out of 5     Points: 2

Hi

Please check the following code:

Design
------

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<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" />
</div>
</form>
</body>
</html>

C# Code Behind
--------------

protected void Button1_Click(object sender, EventArgs e)
{
Response.Write(FileUpload1.PostedFile.ContentLength);
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;
}
else
{
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: 991     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
chirag.madhani@yahoo.com


 
#536234    Author: pavan kumar      Member Level: Bronze      Member Rank: 4886     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.



Next : Doubt in abstract class
Previous : What is the final row count in datatable
Return to Discussion Forum
Post New Message
Category: ASP.NET

Related Messages

Subscribe to Subscribers
Active Members
TodayLast 7 Daysmore...

Awards & Gifts
Talk to Webmaster Tony John
Copyright © SpiderWorks Technologies Pvt Ltd., Kochi, India