简体   繁体   English

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

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

I am working on a file upload functionality which should only allow .xlxs and .docx files. 我正在处理文件上传功能,该功能应该只允许.xlxs和.docx文件。 At the moment with the code below I am able to limit uploads to .xlsx only. 目前使用下面的代码,我只能将上传限制为.xlsx。 I would however like to also upload .docx files but it seems my piece of code is preventing the .docx files from being uploaded. 但是我想上传.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;
      }

So that code above should spit out anything that is not xlsx or docx but the problem is it is spitting out docx as well 所以上面的代码应该吐出任何不是xlsx或docx的东西,但问题是它也在吐出docx

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

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

try your code like this: 尝试你的代码:

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;
}

You're checking if it's not docx AND it's not xlsx, not OR 你正在检查它是不是docx而且它不是xlsx,而不是OR

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将文件限制为仅在Angular文件上传中以UTF-8编码? - How to restrict files to UTF-8 encoded only in Angular file upload? 如何将文件上传更改为仅图像上传,Laravel - How do I change file upload to only image upload, Laravel Nervgh角度文件上传 - 如何限制文件格式说jpeg和png? - Nervgh Angular File Upload - How do I restrict the file formats to say jpeg and png? 如何使用“docxtemplater”生成的多个 docx 文件创建一个 zip 文件? - How do I create a zip file with multiple docx files generated by the "docxtemplater"? 如何仅使用 angular13 限制 pdf 文件的文件上传或拖放,而无需任何第三方工具或插件 - how to restrict file upload or drag and drop for pdf files only using angular13 without any 3rd party tools or plugins 限制 Dropzone 仅上传特定类型的文件 - Restrict Dropzone to upload only specific type of files 我如何限制某些文件格式上传? - How i can restrict some file formats to be upload? 如何访问包含在jquery文件上传插件中准备上载的文件列表的数组/对象? - How do I access an array/object contain a list of files staged for upload in the jquery file upload plugin? 如何将我的节点快速应用程序限制为仅本地主机? - How do i restrict my node express application to localhost only? 如何将用户限制为只有一个帖子请求 - how do I restrict users to only one post request
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM