我知道你可以通过获取验证文件类型,它的使用pop
扩展
EG
var ext = $('#file-input').val().split('.').pop().toLowerCase();
if($.inArray(ext, ['gif','png','jpg','jpeg']) == -1) {
alert('invalid extension!');
}
我也知道,你可以设置为HTML input元素本身只接受特定的文件类型的属性。
EG
<input type="file" accept=".xls,.xlsx" />
但我发现另一种方式来获取文件类型
EG
$('#file').change(function(){
var file = this.files[0];
type = file.type;
});
它只有在新的浏览器工作,虽然,我想知道如何验证特定的文件类型与此,例如只允许pdf
,jpg
,docx
,xlsx
。
我试着在控制台打印file.type
的输出,我得到application/vnd.openxmlformats-officedocument.wordprocessingml.document
为docx type
所以我想它是这样的:
if(file !== 'application/docx'){
alert('Invalid File Type');
}
不幸的是,这是行不通的。
据Office 2007 File Format MIME Types for HTTP Content Streaming,通过开发的文件格式,.docx
文件扩展名,MIME类型公司出版,application/vnd.openxmlformats-officedocument.wordprocessingml.document
是有效的MIME类型的.docx
文件
Ext MIME Type .docx application/vnd.openxmlformats-officedocument.wordprocessingml.document
application/docx
未列出作为用于由主源的MIME type扩展有效.docx
。