<input type='file' name='userFile'>
now when i will click the browse button, the browse dialog will show all files… what if i want to filter file types lets say
- only images or .png & .jpg & .gifs
- only office file like .doc & .docx & .pps
how to do it…
There is an “accept” attribute on file input controls, where you can specify the types of files you want.
From what i’m seeing, though, many browsers like to ignore it — the file types that can be specified are MIME types, so a strictly correct browser would have to look at every file regardless of extension and see if it’s an image (and if so, what type it is).Update: It seems at least some version of every major browser on Windows now provides at least some support for the
acceptattribute. (Even IE supports it, as of version 10.) However, it’s still a bit early yet to rely on it, as IE 8 and 9 still don’t support it. And support in general is a bit spotty.I don’t have Safari, and don’t plan on downloading it. If someone could document Safari’s support…Partial support on safari. http://caniuse.com/#search=acceptYou should consider adding the attribute, so browsers that support it can help the user find the right files more easily. But i would still suggest that you check the filename after the file’s selected and show an error message if a file with the wrong extension is uploaded.
And of course, definitely have the server double-check that the file is the right type. File extensions are just a naming convention, and can be easily subverted. And even if we could trust the browser (which we can’t), and even if it did attempt to filter stuff as you asked (which it might not), the chance of it actually verifying that that
.docfile is truly a Word document is next to nil.