AspdotnetCodes.com
Search
Articles
Books
Resources
Asp.Net News
Downloads
Free Tech Magazines
Asp.Net Web Hosting
Archives
Softwares
Newsletter
Suggest Us
Link to Us
Feeds Subscription
Articles
Questions & Answers
Tips & Tricks
 How to Validate FileUpload control to allow only Image Files using RegularExpressionValidator?
Posted by Moderator1 on  9/18/2009 11:08:12 AM 3459 Views
To upload images to the server from Asp.Net web application, we will use the FileUpload server control. This control can be validated to upload only Image files using the RegularExpressionValidator control.

To achieve this drag and drop a FileUpload and RegularExpressionValidator control into your aspx page. Then set the ControlToValidate property as “FileUpload1” and the ValidationExpression property as specified below.

<asp:FileUpload ID="FileUpload1" runat="server" />

<asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ControlToValidate="FileUpload1"
ErrorMessage="Invalid Image File"
ValidationExpression=
"^([0-9a-zA-Z_\-~ :\\])+(.jpg|.JPG|.jpeg|.JPEG|.bmp|.BMP|.gif|.GIF|.png|.PNG)$">
</asp:RegularExpressionValidator>

So the above RegularExpressionValidator will only allow jpg, jpeg, bmp, gif, png formats image files. You can add more file formats separated by “|” symbol. Also you can specify more special characters after the string “_\-~” to allow the files you want to upload to the server.

Post New Tips/Tricks View all Tips/Tricks

Featured Resources:
Take Advantage of Oracle's 2 Day DBA Course
This course is designed to complement the Oracle 2 Day DBA documentation.
Oracle Magazine
Contains technology strategy articles, sample code, tips, Oracle and partner news, how to articles for developers and DBAs, and more.
100% Free Subscription until Stock lost.View complete list.
Sponsored by