JQuery的如何通过它的使用扩展`file.type`功能得到验证文件类型?

问题描述 投票:0回答:1

我知道你可以通过获取验证文件类型,它的使用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;
});

它只有在新的浏览器工作,虽然,我想知道如何验证特定的文件类型与此,例如只允许pdfjpgdocxxlsx

我试着在控制台打印file.type的输出,我得到application/vnd.openxmlformats-officedocument.wordprocessingml.documentdocx type所以我想它是这样的:

if(file !== 'application/docx'){
    alert('Invalid File Type');
} 

不幸的是,这是行不通的。

javascript jquery
1个回答
0
投票

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

参见What is a correct mime type for docx, pptx etc?

© www.soinside.com 2019 - 2024. All rights reserved.