FileUpload Validation for Images using RegularExpression

Code in ASPX page:

<html xmlns="">
<head runat="server">
<form id="form1" runat="server">
<asp:FileUpload ID="FileUpload1" runat="server" />
<asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server"
ErrorMessage=".jpeg or .JPEG or .gif or .GIF or .png or .PNG Formats only"
<asp:Button ID="Button1" runat="server" onclick="Button1_Click"
style="font-weight: 700; color: #0000CC; background-color: #FFFF99"
Text="Upload" />

Code behind:

protected void Button1_Click(object sender, EventArgs e)
 if (FileUpload1.HasFile)
     string str = FileUpload1.FileName;
     FileUpload1.PostedFile.SaveAs(MapPath("~/Uploads/" + str));


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s