动态将maxfiles添加到dropzone

时间:2018-07-28 22:30:56

标签: dropzone.js dropzone

我在Laravel中使用dropzone,当我单击按钮时,我需要添加最大数量的照片,但这并没有获取变量的值,但是当我更改并放入一个数字时,效果很好。

('#add').on("click", function(e){
  e.preventDefault();
  var max_photos = $("#num_photos").val();
  max_photos = 5 - max_photos;
  $( "#add-photos" ).fadeIn();
});

Dropzone.autoDiscover = false;
var myDropzone2 =  new Dropzone("div#add-photos-edit", {      
      url: "../../update/store2", 
      autoProcessQueue:false,
      parallelUploads: 100,
      uploadMultiple: true,
      maxFilesize:5,
      maxFiles: max_photos,
      acceptedFiles: ".jpeg,.jpg,.png,.gif",
      headers: {
      'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
      },
      init: function() {
          myDropzone2 = this; // closure
          var btn = document.querySelector("#submit");         
          btn.addEventListener("click", function(e) {
              e.preventDefault();
              e.stopPropagation();

          });
          this.on("sending", function(file, xhr, data) {
          });

          this.on("success", function(file, xhr){
            alert(file.xhr.response);

          })
      }

});


$("#submit").click( function(e){
      e.preventDefault();
      myDropzone2.processQueue();
});

1 个答案:

答案 0 :(得分:1)

在初始化dropzone元素之前,很可能没有设置变量max_photos,您可以采用另一种方法来处理它,初始化dropzone然后设置maxFiles选项。

Dropzone.autoDiscover = false;

var myDropzone2 =  new Dropzone("div#add-photos-edit", {
  /*
   * Your configuration
   */
});

$('#add').on("click", function(e){

  var max_photos = $("#num_photos").val();
  max_photos = 5 - max_photos;
  $( "#add-photos" ).fadeIn();

  myDropzone2.options.maxFiles = max_photos;

});