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 13190 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:
Speech Technology Magazine
Speech Technology is recognized worldwide as the leading source of information on speech technology solutions that are changing communications and technology needs of organizations worldwide.
Programmers Heaven C# School Book - Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
100% Free Subscription until Stock lost.View complete list.
Sponsored by