简体   繁体   中英

Getting error Dropzone already attached with angular Directives

I am using following code for drop zone but i am getting error, i tried to debug it but i am not able to resolve this action plz guide

http://jsfiddle.net/anam123/rL6Bh/

 -------------------> "Error: Dropzone already attached.

  throw new Error("Dropzone already attached.");" 

Code::

https://gist.github.com/compact/8118670

snippts:

 /**
 * An AngularJS directive for Dropzone.js, http://www.dropzonejs.com/
 * 
 * Usage:
 * 
 * <div ng-app="app" ng-controller="SomeCtrl">
 *   <button dropzone="dropzoneConfig">
 *     Drag and drop files here or click to upload
 *   </button>
 * </div>
 */

angular.module('dropzone', []).directive('dropzone', function () {
  return function (scope, element, attrs) {
    var config, dropzone;

    config = scope[attrs.dropzone];

    // create a Dropzone for the element with the given options
    dropzone = new Dropzone(element[0], config.options);

    // bind the given event handlers
    _.each(config.eventHandlers, function (handler, event) {
      dropzone.on(event, handler);
    });
  };
});

angular.module('app', ['dropzone']);

angular.module('app').controller('SomeCtrl', function ($scope) {
  $scope.dropzoneConfig = {
    'options': { // passed into the Dropzone constructor
      'url': 'upload.php'
    },
    'eventHandlers': {
      'sending': function (file, xhr, formData) {
      },
      'success': function (file, response) {
      }
    }
  };
});

Solved issue by using following code setup.

So you can either:

  1. Turn off autoDiscover globally like this: Dropzone.autoDiscover = false; , or
  2. Turn off autoDiscover for specific elements like this: Dropzone.options.myAwesomeDropzone = false;

Reference:
FAQ on dropzone

Dropzone.autoDiscover = false;
$('#bannerupload').dropzone({
    url: "/upload",
    maxFilesize: 100,
    paramName: "file",
    maxThumbnailFilesize: 5,
    init: function() {      
      this.on('success', function(file, json) {       
        jQuery("input#mediaid").val(json);
      });
    }
  });

Nothing worked for me so i went to the dropzone.js file and change the line that throw an error (i think in many versions it's in line 426):

if (this.element.dropzone) {
    throw new Error("Dropzone already attached.");
  }

so i replace

throw new Error("Dropzone already attached.");

with

return this.element.dropzone;

and it is working

I was facing same issue "Dropzone already attached" because we enabled myDropzone object in the script and were trying to enable again.

For example

if ($('#upl').attr('class')) {

var myDropzone = new Dropzone("#upl", {init: function () {

and again trying to enable it

 if (jQuery('#password').attr('save_profile')) {
  var myDropzone = new Dropzone("#upl", {init: function () {

with another action.

Please check your code.

Put your create dropzone code inside of a try/catch block

try{
 $('.dropzone').dropzone({
    url: '/upload'
  });
}
catch(error){
    console.log("Catching " + error);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM