【问题标题】:check file type in dropzone检查 dropzone 中的文件类型
【发布时间】:2016-11-17 03:45:56
【问题描述】:

我有以下代码使用 dropzone.js 在某些条件下上传 excel 表,例如最大文件数为 1,并且只接受 excel 类型... 当以正确的文件类型上传多个文件时,首先引发错误并由alert('please enter correct file format') 生成消息,然后只有真正的错误消息由alert('You cannot upload more then 1 file at a time.') 发出警报。所以,我的问题是如何在初始化错误时显示真正的错误消息...

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

【问题讨论】:

    标签: javascript php jquery codeigniter dropzone.js


    【解决方案1】:

    出现错误的文件类型消息是因为您使用 if 语句显然会陷入给出错误的 2 个条件之一。

    所以你应该使用:

    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 上发现了一些东西,实际上只是供将来参考,如果有人需要它github.com/enyo/dropzone/issues/1143
    猜你喜欢
    • 1970-01-01
    • 2011-04-03
    • 1970-01-01
    • 2015-05-04
    • 1970-01-01
    • 2011-08-02
    • 2013-08-26
    • 2018-07-22
    • 1970-01-01
    相关资源
    最近更新 更多