I know you can validate a file type by getting it's extension using pop
e.g.
var ext = $('#file-input').val().split('.').pop().toLowerCase();
if($.inArray(ext, ['gif','png','jpg','jpeg']) == -1) {
alert('invalid extension!');
}
I also know that you can set an attribute to html input element itself to accept only specific file type.
e.g.
<input type="file" accept=".xls,.xlsx" />
but I've found another way to get the file type
e.g.
$('#file').change(function(){
var file = this.files[0];
type = file.type;
});
It's only works in newly browser though, I wanted to know on how to validate specific file type with this, for example allow only pdf, jpg, docx, xlsx.
I've tried to print in console the output of file.type and I get application/vnd.openxmlformats-officedocument.wordprocessingml.document for docx type so I tried it like this:
if(file !== 'application/docx'){
alert('Invalid File Type');
}
Unfortunately, it doesn't work.