Skip to content

Instantly share code, notes, and snippets.

@xgrommx
Forked from JamsMendez/ngUploadDrop.js
Created August 14, 2014 19:53
Show Gist options
  • Save xgrommx/cdbc41a846354330a002 to your computer and use it in GitHub Desktop.
Save xgrommx/cdbc41a846354330a002 to your computer and use it in GitHub Desktop.
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