Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to restrict file type in FileUpload control

Is it possible to allow the fileupload control to show only images?

When we click the Browse button it should show only images.

like image 463
Geeth Avatar asked May 06 '10 10:05

Geeth


People also ask

How do I limit the file type in HTML?

As mentioned in previous answers we cannot restrict user to select files for only given file formats. But it's really handy to use the accept tag on file attribute in html. As for validation, we have to do it at the server side. We can also do it at client side in js but its not a foolproof solution.


2 Answers

In 2015, web browsers support the input accept attribute, so you can do this:

<asp:FileUpload ID="fileUploader" runat="server" accept=".png,.jpg,.jpeg,.gif" /> 

Keep in mind Visual Studio may show you a message about this as an invalid attribute of the FileUpload ASP tool, however.

like image 95
Tonko Boekhoud Avatar answered Sep 28 '22 04:09

Tonko Boekhoud


I found no direct solution for this problem.

This is my workaround using the RegularExpressionValidator:

<asp:FileUpload ID="fuImportImage" runat="server" /> <asp:RegularExpressionValidator ID="regexValidator" runat="server"      ControlToValidate="fuImportImage"      ErrorMessage="Only JPEG images are allowed"       ValidationExpression="(.*\.([Jj][Pp][Gg])|.*\.([Jj][Pp][Ee][Gg])$)"> </asp:RegularExpressionValidator> 
like image 45
Christoph Brückmann Avatar answered Sep 28 '22 05:09

Christoph Brückmann