3 * http://stackoverflow.com/questions/17063000/ng-model-for-input-type-file
5 * After reading, the contents will be available in the scope variable
6 * referred to by container="..."
9 angular.module('egCoreMod')
10 .directive("egFileReader", [function () {
15 link: function (scope, element, attributes) {
16 // TODO: support DataURL, etc. via attrs
17 element.bind("change", function (changeEvent) {
18 var reader = new FileReader();
19 reader.onload = function (loadEvent) {
20 scope.$apply(function () {
21 scope.container = loadEvent.target.result;
24 reader.readAsText(changeEvent.target.files[0]);
30 .directive('egJsonExporter', ['FileSaver', 'Blob', function(FileSaver, Blob) {
37 link: function (scope, element, attributes) {
38 element.bind('click', function (clickEvent) {
39 if (scope.generator) {
40 scope.generator().then(function(value) {
41 var data = new Blob([JSON.stringify(value)], {type : 'application/json'});
42 FileSaver.saveAs(data, scope.defaultFileName);
45 var data = new Blob([JSON.stringify(scope.container)], {type : 'application/json'});
46 FileSaver.saveAs(data, scope.defaultFileName);