Is it possible to allow the fileupload control to show only images?
When we click the Browse button it should show only images.
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.
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.
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With