added web workers

This commit is contained in:
SheetJS 2013-11-22 14:58:11 -08:00
parent 2d0442d364
commit 8561de6e2e
2 changed files with 32 additions and 4 deletions

@ -32,6 +32,19 @@
<script src="jszip.js"></script>
<script src="xlsx.js"></script>
<script>
function xlsxworker(data, cb) {
var worker = new Worker('./xlsxworker.js');
worker.onmessage = function(e) {
switch(e.data.t) {
case 'ready': break;
case 'e': console.error(e.data.d);
case 'xlsx': cb(JSON.parse(e.data.d)); break;
}
};
var arr = btoa(String.fromCharCode.apply(null, new Uint8Array(data)));
worker.postMessage(arr);
}
function get_radio_value( radioName ) {
var radios = document.getElementsByName( radioName );
for( var i = 0; i < radios.length; i++ ) {
@ -111,10 +124,14 @@ function handleDrop(e) {
var name = f.name;
reader.onload = function(e) {
var data = e.target.result;
//var wb = XLSX.read(data, {type: 'binary'});
var arr = String.fromCharCode.apply(null, new Uint8Array(data));
var wb = XLSX.read(btoa(arr), {type: 'base64'});
process_wb(wb);
if(typeof Worker !== 'undefined') {
xlsxworker(data, process_wb);
} else {
//var wb = XLSX.read(data, {type: 'binary'});
var arr = String.fromCharCode.apply(null, new Uint8Array(data));
var wb = XLSX.read(btoa(arr), {type: 'base64'});
process_wb(wb);
}
};
//reader.readAsBinaryString(f);
reader.readAsArrayBuffer(f);

11
xlsxworker.js Normal file

@ -0,0 +1,11 @@
importScripts('jszip.js');
importScripts('xlsx.js');
postMessage({t:"ready"});
onmessage = function (oEvent) {
var v;
try {
v = XLSX.read(oEvent.data, {type: 'base64'});
} catch(e) { postMessage({t:"e",d:e.stack}); }
postMessage({t:"xlsx", d:JSON.stringify(v)});
};