From dbebd21d5e0e4fa4ef925ed82e8e4704575ef288 Mon Sep 17 00:00:00 2001 From: SheetJS Date: Mon, 18 Aug 2014 12:25:02 -0400 Subject: [PATCH] added file input to web demo --- index.html | 38 ++++++++++++++++++++++++++++++++++++-- 1 file changed, 36 insertions(+), 2 deletions(-) diff --git a/index.html b/index.html index b66807e..b8a9ff5 100644 --- a/index.html +++ b/index.html @@ -34,7 +34,8 @@ Output Format: JSON
FORMULAE
--> -
Drop an XLSX or XLSM or XLSB file here to see sheet data.
+
Drop an XLSX or XLSM or XLSB file here to see sheet data
+

... or click here to select a file


Advanced Demo Options:
@@ -182,7 +183,7 @@ function process_wb(wb) { var output = ""; switch(get_radio_value("format")) { case "json": - output = JSON.stringify(to_json(wb), 2, 2); + output = JSON.stringify(to_json(wb), 2, 2); break; case "form": output = to_formulae(wb); @@ -238,6 +239,39 @@ if(drop.addEventListener) { drop.addEventListener('dragover', handleDragover, false); drop.addEventListener('drop', handleDrop, false); } + + +var xlf = document.getElementById('xlf'); +function handleFile(e) { + rABS = document.getElementsByName("userabs")[0].checked; + use_worker = document.getElementsByName("useworker")[0].checked; + var files = e.target.files; + var i,f; + for (i = 0, f = files[i]; i != files.length; ++i) { + var reader = new FileReader(); + var name = f.name; + reader.onload = function(e) { + if(typeof console !== 'undefined') console.log("onload", new Date(), rABS, use_worker); + var data = e.target.result; + if(use_worker) { + xlsxworker(data, process_wb); + } else { + var wb; + if(rABS) { + wb = XLSX.read(data, {type: 'binary'}); + } else { + var arr = fixdata(data); + wb = XLSX.read(btoa(arr), {type: 'base64'}); + } + process_wb(wb); + } + }; + if(rABS) reader.readAsBinaryString(f); + else reader.readAsArrayBuffer(f); + } +} + +if(xlf.addEventListener) xlf.addEventListener('change', handleFile, false);