From 7fee330e32d68558a8f9063fbbe7fc23d343d1e3 Mon Sep 17 00:00:00 2001 From: SheetJS Date: Mon, 28 Jul 2014 13:45:23 -0400 Subject: [PATCH] IE6+ AJAX (fixes #88) --- ajax.html | 8 ++++---- iemagic.js | 37 +++++++++++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+), 4 deletions(-) create mode 100644 iemagic.js diff --git a/ajax.html b/ajax.html index 9d77259..9121d39 100644 --- a/ajax.html +++ b/ajax.html @@ -18,6 +18,7 @@ JS-XLSX AJAX Demo

 
+ @@ -61,11 +62,10 @@ if(typeof Uint8Array !== 'undefined') { process_wb(wb); }; } else { + oReq.setRequestHeader("Accept-Charset", "x-user-defined"); oReq.onreadystatechange = function() { if(oReq.readyState == 4 && oReq.status == 200) { - var data = new VBArray(oReq.responseBody).toArray(); - var arr = new Array(); - for(var i = 0, len = data.length; i != len; ++i) arr[i] = String.fromCharCode(data[i]); - var wb = XLSX.read(arr.join(""), {type:"binary"}); + var ff = convertResponseBodyToText(oReq.responseBody); + var wb = XLSX.read(ff, {type:"binary"}); process_wb(wb); } }; } diff --git a/iemagic.js b/iemagic.js new file mode 100644 index 0000000..c9ac50b --- /dev/null +++ b/iemagic.js @@ -0,0 +1,37 @@ +// http://stackoverflow.com/a/5913807 + +if(/msie/i.test(navigator.userAgent) && !/opera/i.test(navigator.userAgent)) { + var IEBinaryToArray_ByteStr_Script = + "\r\n"+ + "\r\n"; + + // inject VBScript + document.write(IEBinaryToArray_ByteStr_Script); +} + +var convertResponseBodyToText = function (binary) { + var byteMapping = {}; + for ( var i = 0; i < 256; i++ ) { + for ( var j = 0; j < 256; j++ ) { + byteMapping[ String.fromCharCode( i + j * 256 ) ] = + String.fromCharCode(i) + String.fromCharCode(j); + } + } + var rawBytes = IEBinaryToArray_ByteStr(binary); + var lastChr = IEBinaryToArray_ByteStr_Last(binary); + return rawBytes.replace(/[\s\S]/g, + function( match ) { return byteMapping[match]; }) + lastChr; +};