如何将文件上传限制为仅 .xlxs 和 .docx 文件

How do I restrict file upload to only .xlxs and .docx files

我正在开发文件上传功能,它应该只允许 .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

您可以在 HTML 代码中列出受限文件类型:

<input type="file" accept=".xlxs,.docx">

像这样尝试你的代码:

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 AND 它不是 xlsx,不是 OR