Created
March 31, 2015 15:58
-
-
Save alexcastillo/67a77e408902ab1282a4 to your computer and use it in GitHub Desktop.
File Directive
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
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