[英]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.