最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - check file type in dropzone - Stack Overflow

programmeradmin5浏览0评论

I have following code to upload excel sheet using dropzone.js with certain condition such as maximum number of file is 1 and only excel type is accepted... when multiple file is uploaded with correct file type then the error is initiated first and message is generated by alert('please enter correct file format') and then only the real error message is alerted by alert('You cannot upload more then 1 file at a time.') . so, my question is how to show real error message when error is initialized...

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

I have following code to upload excel sheet using dropzone.js with certain condition such as maximum number of file is 1 and only excel type is accepted... when multiple file is uploaded with correct file type then the error is initiated first and message is generated by alert('please enter correct file format') and then only the real error message is alerted by alert('You cannot upload more then 1 file at a time.') . so, my question is how to show real error message when error is initialized...

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);
      }
    });
  },
});
Share Improve this question edited Jul 31, 2020 at 8:28 Kamal Lama asked Jul 14, 2016 at 9:31 Kamal LamaKamal Lama 7002 gold badges7 silver badges21 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 4

The incorrect file type message happens because you with your if statements you will obviously fall into one of the 2 conditions giving the error.

So instead you should use:

switch(file.type)
{
    case 'application/vnd.ms-excel':
    case 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet':
    break;
    default:
    alert('please enter correct file format');
    break;
}
发布评论

评论列表(0)

  1. 暂无评论