Skip to content

Instantly share code, notes, and snippets.

@compact
Last active October 25, 2025 18:19
Show Gist options
  • Select an option

  • Save compact/8118670 to your computer and use it in GitHub Desktop.

Select an option

Save compact/8118670 to your computer and use it in GitHub Desktop.
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) {
}
}
};
});
@TanvirAnowar

Copy link
Copy Markdown

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

@thosakwe

thosakwe commented Jan 3, 2016

Copy link
Copy Markdown

@Sanzeeb: Check out the Dropzone docs.

@vimalavinisha

Copy link
Copy Markdown

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

Xusifob commented Jan 14, 2016

Copy link
Copy Markdown

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

Pitu commented Jan 26, 2016

Copy link
Copy Markdown

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

Copy link
Copy Markdown

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

Copy link
Copy Markdown

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

@digitlimit

Copy link
Copy Markdown

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

@chinmay235

chinmay235 commented Nov 8, 2016

Copy link
Copy Markdown

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

hadijaveed commented Feb 2, 2017

Copy link
Copy Markdown

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

@AsanMohamed

Copy link
Copy Markdown

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