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 var name = scope.defaultFileName || 'evergreen-json-export';
39 element.bind('click', function (clickEvent) {
40 if (scope.generator) {
41 scope.generator().then(function(value) {
42 var data = new Blob([JSON.stringify(value)], {type : 'application/json'});
43 FileSaver.saveAs(data, name);
46 var data = new Blob([JSON.stringify(scope.container)], {type : 'application/json'});
47 FileSaver.saveAs(data, name);
54 // The following directives use a attr instead of binding to get the default file name!
55 .directive('egStringExporter', ['FileSaver', 'Blob', function(FileSaver, Blob) {
63 link: function (scope, element, attributes) {
64 var type = scope.contentType || 'text/plain';
65 var name = scope.defaultFileName || 'evergreen-string-export';
66 element.bind('click', function (clickEvent) {
67 if (scope.generator) {
68 scope.generator().then(function(value) {
69 var data = new Blob([value], {type : type});
70 FileSaver.saveAs(data, name);
73 var data = new Blob([scope.string], {type : type});
74 FileSaver.saveAs(data, name);
81 .directive('egLineExporter', ['FileSaver', 'Blob', function(FileSaver, Blob) {
88 link: function (scope, element, attributes) {
89 element.bind('click', function (clickEvent) {
90 var type = scope.contentType || 'text/plain';
91 var fname = scope.defaultFileName || 'evergreen-string-export';
94 scope.jsonArray.map(function (line) {
95 return JSON.stringify(line) + '\n';