The accept parameter is used to let the browser know it should ask the OS for a file selector dialog which would accept only these type of files.
So this is actually an OS feature.
Now, when you drag&drop a file, the browser doesn't require to open a file selector dialog, so this attribute is not used.
But you can still listen to the change event and do your checking there.
inp.onchange = function(e){
e.preventDefault();
var file = this.files[0];
if(file.type.indexOf('image/') !== 0) {
this.value = null;
console.log("invalid");
}
else {
console.log('valid file');
}
}
Drop a file on this input. <br>
<input type="file" accepts="image/*" id="inp">
Important notes
accepts
, just like MIME Type checking is only done against the file extension, i.e really easily spoofable, you can try a magic-number check, but you should anyway always check from the server.
- The above snippet will work only if your user uploaded a single file. I don't know what you want to do in case they upload a folder or multiple files, but beware that they'll be able to do it, and thus that you will have to handle these cases.