Skip to content

Instantly share code, notes, and snippets.

@alexcastillo
Created March 31, 2015 15:58
Show Gist options
  • Save alexcastillo/67a77e408902ab1282a4 to your computer and use it in GitHub Desktop.
Save alexcastillo/67a77e408902ab1282a4 to your computer and use it in GitHub Desktop.
File Directive
angular.module('Aether').directive('onReadFile', function($parse, Constants) {
return {
restrict : 'A',
scope : false,
link : function(scope, element, attrs) {
var fn = $parse(attrs.onReadFile);
element.on('change', function(onChangeEvent) {
var file = (onChangeEvent.srcElement || onChangeEvent.target).files[0];
var reader = new FileReader();
reader.onload = function(onLoadEvent) {
scope.$apply(function() {
fn(scope, (file.size <= Constants.keysFileSizeLimit) ? {
$fileContent : onLoadEvent.target.result
} : false);
});
};
reader.readAsBinaryString(file);
});
}
};
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment