Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
AngularJS directive for Dropzone.js
/**
* 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
angular.forEach(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) {
}
}
};
});
@nmabhinandan

This comment has been minimized.

Copy link

commented Jul 9, 2014

what is _.each() ?

@hoandang

This comment has been minimized.

Copy link

commented Jul 17, 2014

@nmabhinandan: that is from underscorejs

@guangbochen

This comment has been minimized.

Copy link

commented Jul 19, 2014

nice directive, but it's better to use angular.forEach rather than _.each
#23, angular.forEach(config.eventHandlers, function (handler, event) { ... }

@compact

This comment has been minimized.

Copy link
Owner Author

commented Jul 21, 2014

@guangbochen Thanks, updated.

@visarut-tm

This comment has been minimized.

Copy link

commented Aug 13, 2014

thanks man

@srph

This comment has been minimized.

Copy link

commented Sep 8, 2014

Any chance of turning this into a repository / project instead?

@darklow

This comment has been minimized.

Copy link

commented Oct 7, 2014

Small correction:

'sending': function (file, xhr, formData) {

should be

'sending': function (file, formData, xhr) {

Thanks for nice directive!

@cbeulke

This comment has been minimized.

Copy link

commented Oct 7, 2014

That really made my day! :-)

@ankurshah32

This comment has been minimized.

Copy link

commented Jan 12, 2015

I am using this directive in angularJS but some reason i am not able to include it. Please have a look at my code. As i am new to angular JS. Can any one please help me out in this.
If you can provide me a link excat which any one of you are using for DropzoneJS version and emmiter.js its will be very helpful.
<b

</title>





Drag and drop files here or click to upload




<!--script type="text/javascript" src="customjs/customdropzone.js" >





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

customDropzone.js file
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 DragDropZone(element[0], config.options);

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

};
});

Node server.js File

var app = express();
var path = require('path');
var rootPath = path.normalize(__dirname + '/');
app.use(express.static(rootPath));

app.get('/', function(req, res){
//res.sendfile('index.html');
res.redirect("index.html");
});

app.listen(3000);

@kgiszewski

This comment has been minimized.

Copy link

commented Feb 2, 2015

Thanks a bunch!

@mloparco

This comment has been minimized.

Copy link

commented Feb 20, 2015

Great directive, thank you for creating it! I'm able to get it working in Angular just fine functionally, uploading to Google App Engine on the backend. One issue I'm having is with the appearance - it looks nothing like the original Dropzone by enzo. I tried adding a 'previewTemplate' option to $scope.dropzoneConfig, but that didn't work. And when I don't use a previewTemplate, I assume that the default dropzone appearance should appear, but it doesn't. Please advise, thank you!

@Skyscreepa

This comment has been minimized.

Copy link

commented Feb 20, 2015

Thanks its works perfectly ! But anyone know how to access to function like .processQueue ? I'd like to have a button who is the trigger for the upload !

@arron21

This comment has been minimized.

Copy link

commented Feb 23, 2015

This is awesome thank you so much for making this. Easy to integrate into my current app by replacing the
angular.module('dropzone', [])
with the variable assigned to my app ex: app = angular.module('myApp', [])

@arron21

This comment has been minimized.

Copy link

commented Mar 3, 2015

Does anyone have any insight on how to integrate this with additional FORM elements?

@arron21

This comment has been minimized.

Copy link

commented Mar 3, 2015

@Skyscreepa
You have to declare a variable in the init set to yourVar = this;
then attach the button to the event example code below.

$scope.dropzoneConfig = {
'options': { // passed into the Dropzone constructor
'url': 'upload.php',
'maxFiles': 1,
'autoProcessQueue': false,
init: function(){
dz = this;
$("#submit-all").click(function(){
//$(dz).processQueue();
dz.processQueue();
});
}
}
};

@JacobWDavidson

This comment has been minimized.

Copy link

commented Mar 4, 2015

I'm working on adding two dropzones to my page. The problem I'm running into is having two processQueue buttons. Everything works on the first dropzone instance, but the buttons don't work on the second instance. I tried creating two different dropzones in my controller and assigning separate ids and everything, but no dice. Any tips?

@spoof3r

This comment has been minimized.

Copy link

commented Mar 19, 2015

I got it to run, but the styling is way off. Are there any tricks to getting the styling to look as pretty as the original site? His looks awesome! Please advise!

@asif22

This comment has been minimized.

Copy link

commented Apr 11, 2015

@darklow please check: http://www.dropzonejs.com/#event-sending
'sending': function (file, xhr, formData) is the correct format.

@nelsontubaina

This comment has been minimized.

Copy link

commented Aug 3, 2015

I'm having trouble with this part here:
config = scope[attrs.dropzone];

it gives error: Error: config is undefined

--> SORRY I WAS BEING A COMPLE ASS WIPE. IT WORKS LIKE A LICKY CHARM! THE LUCK O' THE IRISH! THANK YOU SO VERY MUCH!

@myPrecious82

This comment has been minimized.

Copy link

commented Sep 14, 2015

@nelsontubaina I'm getting the same error. What am I missing? Thanks!

@AndreXmuniz

This comment has been minimized.

Copy link

commented Nov 20, 2015

Hi guys, im havin some troubles calling the directive

Drag and drop files here or click to upload

is there anyway to call this but instead of passing dropzoneConfig, pass the default options?,

something like this

when i dont pass anything i get this error
TypeError: Cannot read property 'options' of undefined
im trying to get rid of the scope, cause im already using one in my code,
any replies would help

@secabhi

This comment has been minimized.

Copy link

commented Dec 17, 2015

hi I have one question . if I have to pass url dynamic . how we can implement that

@sanzeeb

This comment has been minimized.

Copy link

commented Jan 2, 2016

how to limit only one file upload and make it replace with new dropped file with old one ?

@thosakwe

This comment has been minimized.

Copy link

commented Jan 3, 2016

@sanzeeb: Check out the Dropzone docs.

@vimalavinisha

This comment has been minimized.

Copy link

commented Jan 5, 2016

Hi,
I need to use file upload action in several jades. Should i need to write dropzoneconfig for each controller. Is there any way to make this configuration object as global in angular?

@Xusifob

This comment has been minimized.

Copy link

commented Jan 14, 2016

I would have put this in the begining

var atts = attrs.dropzone.split('.');

    var config = scope;
    angular.forEach(atts,function(value,key){
        config = config[value];
    });

    // Get the template
    $.get(config.options.template).then(function(template){

So that if you send an array in your template and not only a scope var (variable from a controller for example) it will still work

@Pitu

This comment has been minimized.

Copy link

commented Jan 26, 2016

Would it be possible to attach the success function response to the ng-model on the view? Something along the lines of

<div class="dropzone" dropzone="dropzoneConfig" ng-model="response"></div>
@barrycarey

This comment has been minimized.

Copy link

commented Apr 13, 2016

Can anyone advise me on how to update scope variables via the dropzone event handles? It seems you can access scope objects but can't update them from the callback.

@thatisuday

This comment has been minimized.

Copy link

commented Apr 28, 2016

Try new AngularJS directive for Dropzone https://github.com/thatisuday/ngDropzone

@digitlimit

This comment has been minimized.

Copy link

commented Jul 27, 2016

Nice. How do I send uploaded file along with other form inputs. I don't want instant upload

@chinmay235

This comment has been minimized.

Copy link

commented Nov 8, 2016

I have used AngularJS Inspinia admin template. I saw here original inspinia file dropzone not working properly. If I go other page to uploadfile page the dropzone not working. If I refreshed in that page. then dropzone working fine. Please tell me what is the issue behind that dropzone?

@hadijaveed

This comment has been minimized.

Copy link

commented Feb 2, 2017

How is angular.forEach is better than _.each(). I prefer to use underscore.js or lodash.js.

@AsanMohamed

This comment has been minimized.

Copy link

commented Feb 13, 2017

Hi, How to add delete URL to delete the file? and Icon delete (X) is not deleting. Any help would be appreciated.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.