From de1fcc9712bfb244073e416c22f9635d3030ab1c Mon Sep 17 00:00:00 2001 From: gandhimathi Date: Mon, 27 Aug 2018 15:37:52 +0530 Subject: [PATCH] xls to json convert directives added --- .../js/directives/convert_Json/convertJson.js | 72 +++++++++++++++++++ Apollo/index.html | 5 +- 2 files changed, 76 insertions(+), 1 deletion(-) create mode 100644 Apollo/assets/js/directives/convert_Json/convertJson.js diff --git a/Apollo/assets/js/directives/convert_Json/convertJson.js b/Apollo/assets/js/directives/convert_Json/convertJson.js new file mode 100644 index 00000000..1717f597 --- /dev/null +++ b/Apollo/assets/js/directives/convert_Json/convertJson.js @@ -0,0 +1,72 @@ +'use strict'; +app.directive('jsXls', function () { + return { + restrict: 'E', + template: '', + replace: true, + link: function (scope, element, attrs) { + + function handleSelect() { + var files = this.files; + for (var i = 0, f = files[i]; i != files.length; ++i) { + var reader = new FileReader(); + var name = f.name; + reader.onload = function (e) { + if (!e) { + var data = reader.content; + } else { + var data = e.target.result; + } + + /* if binary string, read with type 'binary' */ + try { + var workbook = XLS.read(data, { type: 'binary' }); + + if (attrs.onread) { + var handleRead = scope[attrs.onread]; + if (typeof handleRead === "function") { + handleRead(workbook); + } + } + } catch (e) { + if (attrs.onerror) { + var handleError = scope[attrs.onerror]; + if (typeof handleError === "function") { + handleError(e); + } + } + } + + // Clear input file + element.val(''); + }; + + //extend FileReader + if (!FileReader.prototype.readAsBinaryString) { + FileReader.prototype.readAsBinaryString = function (fileData) { + var binary = ""; + var pt = this; + var reader = new FileReader(); + reader.onload = function (e) { + var bytes = new Uint8Array(reader.result); + var length = bytes.byteLength; + for (var i = 0; i < length; i++) { + binary += String.fromCharCode(bytes[i]); + } + //pt.result - readonly so assign binary + pt.content = binary; + $(pt).trigger('onload'); + } + reader.readAsArrayBuffer(fileData); + } + } + + reader.readAsBinaryString(f); + + } + } + + element.on('change', handleSelect); + } + }; +}); \ No newline at end of file diff --git a/Apollo/index.html b/Apollo/index.html index a9f273cc..b92f1e8e 100755 --- a/Apollo/index.html +++ b/Apollo/index.html @@ -130,7 +130,10 @@ - + + + +