I am using input type="file", Now my requirement is that I only want to select png images, that is when I select browse a "png" filter should be applied.
I have referred www.w3schools.com/tags/att_input_accept.asp
and below is the code I am using, this works fine with Chrome but does not work with Firefox and IE.
Please can anyone help me understand what wrong I must be doing ?
<h2>Below uses accept="image/*"</h2>
<input type="file" name="pic1" accept="image/*" />
<h2>Below I need to accept only for png</h2>
<input type="file" name="pic1" accept="image/png" />
Here is a fiddle link to it http://jsfiddle.net/Jcgja/2/
This is just the validation of the type of file. Not the whole upload script.
You need to validate it through java script. Below is the code for java script validation
As pointed out in the comments, accepted solution won't work with a file name with several "." in it. This should handle it better, it is more bug-proof and flexible, and you can manage accepted extensions by editing the array.
The browser support information on that page of w3schools is not correct.
If you check this page, you see that the
accept
attribute it's not implemented in Firefox:https://developer.mozilla.org/en/HTML/Element/Input
Update:
The
accept
attribute is now implemented in Firefox, but users who don't have a recent version still won't have the support.you can use javascript function onChane event of a file function
As you can see, the 'accept' attribute is not properly supported by any of the major browsers. You could use a javascript validation on the form onsubmit event to verify if the file type is correct, returning false otherwise.
Do not use this attribute as a validation tool. File uploads should be validated on the server.