Javascript 检查dropzone中的文件类型
我有以下代码使用dropzone.js上传excel工作表,并满足某些条件,例如最大文件数为1,并且只接受excel类型。。。 当以正确的文件类型上载多个文件时,首先会启动错误,并通过Javascript 检查dropzone中的文件类型,javascript,php,jquery,codeigniter,dropzone.js,Javascript,Php,Jquery,Codeigniter,Dropzone.js,我有以下代码使用dropzone.js上传excel工作表,并满足某些条件,例如最大文件数为1,并且只接受excel类型。。。 当以正确的文件类型上载多个文件时,首先会启动错误,并通过警报(“请输入正确的文件格式”)生成消息,然后通过警报(“您不能一次上载多个文件”)只会发出真正的错误消息。。所以,我的问题是如何在错误初始化时显示真正的错误消息 var myDropzone = new Dropzone("div#myAwesomeDropzone", { url: &q
警报(“请输入正确的文件格式”)
生成消息,然后通过警报(“您不能一次上载多个文件”)只会发出真正的错误消息。
。所以,我的问题是如何在错误初始化时显示真正的错误消息
var myDropzone = new Dropzone("div#myAwesomeDropzone", {
url: "<?php echo base_url(); ?>test/upload_excel_file",
maxFiles: 1,
acceptedFiles: ".xls,.xlsx",
dictDefaultMessage:
"Drag an excel sheet here to upload, or click to select one",
init: function () {
this.on("maxfilesexceeded", function (file) {
alert("You cannot upload more then 1 file at a time.");
this.removeFile(file);
});
this.on("error", function (file) {
var type = file.type;
//alert(type);
if (type != "application/vnd.ms-excel") {
alert("please enter correct file format");
this.removeFile(file);
} else if (
type !=
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
) {
alert("please enter correct file format");
this.removeFile(file);
}
});
},
});
var myDropzone=新的Dropzone(“div#myAwesomeDropzone”{
url:“测试/上传excel文件”,
maxFiles:1,
接受的文件:“.xls、.xlsx”,
默认信息:
“将excel工作表拖到此处上载,或单击选择一个”,
init:函数(){
此.on(“MaxFilesExcepended”,函数(文件){
警报(“一次上载的文件不能超过1个。”);
此.removeFile(文件);
});
此.on(“错误”,函数(文件){
var type=file.type;
//警报(类型);
如果(类型!=“应用程序/vnd.ms excel”){
警报(“请输入正确的文件格式”);
此.removeFile(文件);
}否则如果(
打字=
“application/vnd.openxmlformats of iceDocument.spreadsheetml.sheet”
) {
警报(“请输入正确的文件格式”);
此.removeFile(文件);
}
});
},
});
发生错误的文件类型消息是因为使用if语句时,您显然会陷入导致错误的两种情况之一
因此,您应该使用:
switch(file.type)
{
case 'application/vnd.ms-excel':
case 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet':
break;
default:
alert('please enter correct file format');
break;
}
我想你在IE11上没有遇到任何问题吧?我目前面临一个问题,类型只是一个空字符串,但我在其他浏览器中没有类似的问题。在网上也很难找到与此相关的信息。谢谢我刚刚在他们的Github上找到了一些东西,实际上只是为了将来有人需要的时候参考