我正在研究一个文件上传功能,应该只允许.xlxs和.docx文件。目前,与下面的代码,我可以限制上传到.xlsx只。然而,我也想上传.docx文件,但我的代码似乎阻止了.docx文件的上传。
if (files.length > 0) {
if (files[0].name.lastIndexOf('.docx') === -1 || files[0].name.lastIndexOf('.xlsx') === -1) {
$.msgbox("Please note: only excel file formats are allowed. Please download the provided upload template, see the link below.");
this.value = '';
return;
}
因此,上面代码应该输出除xlsx或docx之外的任何内容,但问题是它也输出了docx
发布于 2019-02-28 01:38:26
像这样试一下你的代码:
if (files[0].name.lastIndexOf('.docx') === -1 && files[0].name.lastIndexOf('.xlsx') === -1) {
$.msgbox("Please note: only excel file formats are allowed. Please download the provided upload template, see the link below.");
this.value = '';
return;
}
您正在检查它是否不是docx,也不是xlsx,也不是OR
发布于 2019-02-28 01:34:20
您可以在HTML代码中列出受限制的文件类型:
<input type="file" accept=".xlxs,.docx">
https://stackoverflow.com/questions/54911239
复制相似问题