我有以下代码使用dropzonejs上传excel表格,其中某些条件如最大文件数为1且只接受excel类型...
当使用正确的文件类型上载多个文件时,首先启动错误,然后由alert('please enter correct file format')
生成消息,然后alert('You cannot upload more then 1 file at a time.')
仅警告实际错误消息。所以,我的问题是如何在初始化错误时显示真实的错误消息...
<script>
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);
}
});
}
});
</script>
答案 0 :(得分:2)
发生了错误的文件类型消息,因为您使用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;
}