首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何将文件上载限制为仅上传.xlxs和.docx文件

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

Stack Overflow用户
提问于 2019-02-28 01:28:50
回答 2查看 536关注 0票数 1

我正在研究一个文件上传功能,应该只允许.xlxs和.docx文件。目前,与下面的代码,我可以限制上传到.xlsx只。然而,我也想上传.docx文件,但我的代码似乎阻止了.docx文件的上传。

代码语言:javascript
运行
复制
 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

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2019-02-28 01:38:26

像这样试一下你的代码:

代码语言:javascript
运行
复制
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

票数 1
EN

Stack Overflow用户

发布于 2019-02-28 01:34:20

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

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

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54911239

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档