Filereader.readasbinarystring() Is Not Support Ie 10, 11
The below code works in chrome browser. $('#file').change(function(e) { var fileReader = new FileReader(), file = e.target.files[0]; if (typeof fileReader.read
Solution 1:
All though the documentation says that the readAsBinaryString
function is defined, but it still gives Can't be resolved
and function definition not found
errors.
You can try this code. It has worked for me. Please refer my comments for the help.
I have tested the code in IE 11 and Chrome.
Html Code
<html><head><scriptsrc="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script><scriptsrc="https://cdnjs.cloudflare.com/ajax/libs/xlsx/0.8.0/jszip.js"></script><scriptsrc="https://cdnjs.cloudflare.com/ajax/libs/xlsx/0.8.0/xlsx.js"></script><scriptsrc="index.js"></script></head><body><inputtype="file"id="files"onchange="handleFileSelect(event)"/><outputid="list"></output></body></html>
Javascript Code
//readAsBinaryString function is not defined in IE//Adding the definition to the function prototypeif (!FileReader.prototype.readAsBinaryString) {
console.log('readAsBinaryString definition not found');
FileReader.prototype.readAsBinaryString = function (fileData) {
var binary = '';
var pk = this;
var reader = newFileReader();
reader.onload = function (e) {
var bytes = newUint8Array(reader.result);
var length = bytes.byteLength;
for (var i = 0; i < length; i++) {
var a = bytes[i];
var b = String.fromCharCode(a)
binary += b;
}
pk.content = binary;
$(pk).trigger('onload');
}
reader.readAsArrayBuffer(fileData);
}
}
functionhandleFileSelect(evt) {
console.log(evt);
var reader = newFileReader();
reader.onload = function(e){
if (reader.result)
reader.content = reader.result;
//In IE browser event object is nullvar data = e ? e.target.result : reader.content;
var baseEncoded = btoa(data);
var wb = XLSX.read(baseEncoded, {type: 'base64'});
processWorkbook(wb);
};
reader.onerror = function(ex){
console.log(ex);
};
//I'm reading the first file//You can modify it as per your needconsole.log(evt.target.files[0]);
reader.readAsBinaryString(evt.target.files[0]);
}
functionprocessWorkbook(workbook) {
console.log(workbook.Sheets['sheet_name']['excel_cell_name_to_be_accessed'].v);
//For exampleconsole.log(workbook.Sheets['sheet1']['C2'].v);
//you can iterate through all the sheetsfor(var i = 0; i < workbook.SheetNames.length; i++) {
workbook.SheetNames[i]['cell_name_to_be_accessed'] //rest of the processing
}
//You can now work with the workbook as per your requirements
}
Post a Comment for "Filereader.readasbinarystring() Is Not Support Ie 10, 11"