Skip to content

Instantly share code, notes, and snippets.

@JamsMendez
Created August 12, 2014 05:00
Show Gist options
  • Save JamsMendez/c6f2012830b127d6070d to your computer and use it in GitHub Desktop.
Save JamsMendez/c6f2012830b127d6070d to your computer and use it in GitHub Desktop.
Directiva de Angular.js para subir archivos Drag on Drop
directives.uploadDrop = function (){
return {
require: '?ngModel',
restrict: 'E',
template: '<div class="input-file" style="width: 150px;"><span class="glyphicon glyphicon-picture"></span></div>',
link: function (scope, element, attrs, ngModel){
if (!ngModel) return;
element.bind('drop', function (event) {
event.stopPropagation();
event.preventDefault();
event.originalEvent.dataTransfer.dropEffect = 'copy';
var file = event.originalEvent.dataTransfer.files[0];
reader = new FileReader();
reader.onload = function (event) {
var dataURL = reader.result;
var image = file;
scope.$apply(function() {
ngModel.$setViewValue({ src: dataURL, file: file });
});
};
reader.readAsDataURL(file);
return false;
});
element.bind('dragover', function (event) {
event.stopPropagation();
event.preventDefault();
event.originalEvent.dataTransfer.dropEffect = 'copy';
});
}
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment