Created
March 14, 2014 01:12
-
-
Save locnguyen/9540442 to your computer and use it in GitHub Desktop.
AngularJS directive for drag and drop file uploading
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
(function () { | |
angular.module('ui').directive('imageDrop', function () { | |
return { | |
restrict: 'EA', | |
scope: { | |
dropFn: '&' | |
}, | |
link: function (scope, element, attrs) { | |
element.bind('dragover', function (e) { | |
stopExecution(e); | |
}); | |
element.bind('dragleave', function (e) { | |
stopExecution(e); | |
}); | |
element.bind('drop', function (e) { | |
stopExecution(e); | |
e.dataTransfer = e.originalEvent.dataTransfer; | |
var file = e.dataTransfer.files[0], | |
reader = new FileReader(); | |
reader.onload = function (event) { | |
element.attr('style', 'background: url(' + event.target.result + ') no-repeat top center;background-size: 100%;'); | |
scope.$apply(function() { | |
scope.dropFn({ encodedImage: event.target.result, filename: file.name }); | |
}); | |
}; | |
reader.readAsDataURL(file); | |
}); | |
function stopExecution (e) { | |
e.preventDefault(); | |
e.stopPropagation(); | |
} | |
} | |
} | |
}) | |
}()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment