2

I have to upload images into DB using file up-loader. I have used the given below code, but it is not working. If I upload any pdf or doc file that also inserting into DB and the error message also displaying. I want to stop insertion if the file is not an image format. Help me to find a proper solution. Thank you.

ASPX :

<asp:FileUpload ID="PhotoUpload1" runat="server" ForeColor="#999999" />
<asp:RegularExpressionValidator ID="RegularExpressionValidator6" runat="server" ErrorMessage="*jpeg,gif,png" ControlToValidate="PhotoUpload1" ForeColor="Red" 
ValidationExpression=".*\.([gG][iI][fF]|[jJ][pP][gG]|[jJ][pP][eE][gG]|[bB][mM][pP])$">
</asp:RegularExpressionValidator>


<asp:Button ID="btnSubmit" runat="server" OnClick="btnSubmit_Click" Text="Submit" />
Vipin
  • 261
  • 6
  • 20
  • 44

2 Answers2

5

This should work for jpg, png, gif and png.

UPDATED

/.*\.(gif|jpe?g|bmp|png)$/igm

You can check how it works here

Emre Acar
  • 920
  • 9
  • 24
1

Try alternation "|" notation. And only list allowed file extensions (whitelist) like jpg,png.

<asp:FileUpload ID="PhotoUpload1" runat="server" ForeColor="#999999" />
<asp:RegularExpressionValidator ID="RegularExpressionValidator6" runat="server" ErrorMessage="*jpeg,gif,png" ControlToValidate="PhotoUpload1" ForeColor="Red" 
ValidationExpression="(.*png$)|(.*jpg$)|(.*jpeg$)">
</asp:RegularExpressionValidator>

Asp.Net validation need to be validated on server side too.

    protected void btnSubmit_Click(object sender, EventArgs e)
    {
       //Note that there might be ServerSideValidation which evaluated to false.
       if (!Page.IsValid)  
         return;

       // Insert DB code here.
    }
Community
  • 1
  • 1
Atilla Ozgur
  • 14,339
  • 3
  • 49
  • 69
  • I want insert only image file into DB. These answers are showing error message if the selected file is not a image format while clicking insert button, but the file also inserting into db – Vipin Dec 08 '14 at 07:16
  • The given below code is used to insert data into DB. QA_Form2TableAdapters.tbl_QA_InstallationTableAdapter qa; qa =new QA_Form2TableAdapters.tbl_QA_InstallationTableAdapter(); qa.InsertAll(ddlState.Text, ddlDistrict.Text,PhotoUpload1.FileBytes); – Vipin Dec 08 '14 at 07:42