Subscribe to Subscribers
Talk to Webmaster Tony John

Forums » .NET » ASP.NET »

How to validate file size in file upload control


Posted Date: 13 Jul 2012      Posted By:: Madhus     Member Level: Silver    Member Rank: 998     Points: 1   Responses: 4



hi ,
i want to validate file upload control,maximum upload file size is 50Mb

how can i validate

Thanks,
Madhu.




Responses

#680165    Author: bulli guruvu setty      Member Level: Gold      Member Rank: 335     Date: 13/Jul/2012   Rating: 2 out of 52 out of 5     Points: 4

Hi Madhu,

Please find the below C# function for validating file size:

protected void getfilesize(object source, ServerValidateEventArgs args)
{
string data = args.Value;
args.IsValid = false;
double filesize = FileUploadControl1.FileContent.Length;
if (filesize > 5000)
{
args.IsValid = false;
}
else
{
args.IsValid = true;
}
}

Note:

Here "5000" is equal to 5kb.Value should be in bytes.

Thanks & Regards
D.Bulli Guruvu Setty



 
#680191    Author: Anil Kumar Pandey      Member Level: Platinum      Member Rank: 1     Date: 13/Jul/2012   Rating: 2 out of 52 out of 5     Points: 3

You can try this,


int fileSize = FileUpload1.PostedFile.ContentLength;
if (fileSize > (maxFileSize * 1024))
{
PanelError.Visible = true;
lblError.Text = "Filesize of image is too large. Maximum file size permitted is " + maxFileSize + "KB";
return;
}



Thanks & Regards
Anil Kumar Pandey
Microsoft MVP, DNS MVM



 
#680196    Author: chandramohan      Member Level: Gold      Member Rank: 221     Date: 13/Jul/2012   Rating: 2 out of 52 out of 5     Points: 3

protected void Application_BeginRequest(object sender, EventArgs e)
{
//This value is in bytes.
int iMaxFileSize = 2097152;
if (Request.ContentLength > iMaxFileSize)
{
Response.Redirect("~/Pages/Error_Upload.aspx?size1=" + Request.ContentLength);
}
}






 
#680297    Author: Ravindran        Member Level: Diamond      Member Rank: 3     Date: 13/Jul/2012   Rating: 2 out of 52 out of 5     Points: 4

Try like below code


Select Your File <asp:FileUpload ID="FileUpload1" runat="server" /><br />
<asp:CustomValidator ID="CustomValidator1" runat="server"
ErrorMessage="Please select valid .jpg or .jpeg file and less than 1MB"
onservervalidate="CustomValidator1_ServerValidate"></asp:CustomValidator><br />
<asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />


server side

protected void CustomValidator1_ServerValidate(object source, ServerValidateEventArgs args)
{
string filename="";
if (FileUpload1.HasFile)
{
filename = FileUpload1.PostedFile.FileName;
}
else
{
args.IsValid = false;
}

if (filename == "")
{
// check file is empty or not
args.IsValid = false;
}
//Check file less then 1MB multipy for 50*1024 Mb
if ((FileUpload1.PostedFile.ContentLength / 1024) > 1024)
{
args.IsValid = false;
}
}


Regards
N.Ravindran
Your Hard work never fails



 
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 : How to export dynamically created table to excel?
Previous : How to show excel sheets names into listbox after uploading the file??
Return to Discussion Forum
Post New Message
Category:

Related Messages
Active Members
TodayLast 7 Daysmore...

Awards & Gifts
Copyright © SpiderWorks Technologies Pvt Ltd., Kochi, India
2005 - 2012 All Rights Reserved.
.NET and other trademarks mentioned in this site belong to Microsoft and other respective trademark owners.
Articles, tutorials and all other content offered here is for educational purpose only.
We are not associated with Microsoft or its partners.