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

javascript - Dropzone "addedfile" is not fired when adding images programatically - Stack Overflow

programmeradmin6浏览0评论

I want to add checkbox to dropzone images so that I can upload only the selected ones. I am going to add a checkbox next to each image when the addedfile event is fired.

I am using the following method to programmatically add images to dropzone:

var mockFile = { name: "image.jpg", size: 12345 };
imgUrl = ".jpg"
myDropzone.options.addedfile.call(myDropzone, mockFile);
myDropzone.options.thumbnail.call(myDropzone, mockFile, imgUrl);

The problem is that the "addedfile" event is not fired for images added this way:

myDropzone.on("addedfile", function(file) {
    console.log("file added!"); // this line is never printed
});

But if added an image by manually clicking on dropzone and selecting a file, the above event would fire. Why is this event not fired in the first case?

I want to add checkbox to dropzone images so that I can upload only the selected ones. I am going to add a checkbox next to each image when the addedfile event is fired.

I am using the following method to programmatically add images to dropzone:

var mockFile = { name: "image.jpg", size: 12345 };
imgUrl = "http://example./image.jpg"
myDropzone.options.addedfile.call(myDropzone, mockFile);
myDropzone.options.thumbnail.call(myDropzone, mockFile, imgUrl);

The problem is that the "addedfile" event is not fired for images added this way:

myDropzone.on("addedfile", function(file) {
    console.log("file added!"); // this line is never printed
});

But if added an image by manually clicking on dropzone and selecting a file, the above event would fire. Why is this event not fired in the first case?

Share Improve this question edited Apr 4, 2020 at 21:43 Brian Tompsett - 汤莱恩 5,89372 gold badges61 silver badges133 bronze badges asked Feb 16, 2015 at 15:44 MeysamMeysam 18.1k44 gold badges141 silver badges230 bronze badges 1
  • I don't see anything wrong here. Perhaps the error is within some of the adjacent code? Not sure about Dropzone, but I know typical drag'n'drop functionality requires a lot of e.preventDefault and e.stopPropagation for event listeners to work properly. – Ross Brasseaux Commented Feb 16, 2015 at 15:52
Add a ment  | 

4 Answers 4

Reset to default 8

Don't do what I did and try to listen for the addedFile event instead of addedfile (d'oh).

According to dropzone documentation: The proper way to trigger addedfile event is to call it in the init section when you instantiate dropzone.

Example:

   Dropzone.options.myAwesomeDropzone = {
      init: function() {
       this.on("addedfile", function(file) { alert("Added file."); });
      }
   };

i had same issue and found the answer here from @enyo: https://github./enyo/dropzone/issues/209

Basically the addedfile.call is a bit clumsy and you need replace this line by the following:

myDropzone.emit("addedfile", file);

This way the "addedfile" function is called properly.

Cheers

Must call myDropzone.options.addedfile.call(myDropzone, mockFile); after declared "addedfile" callback.

发布评论

评论列表(0)

  1. 暂无评论