FileReader.readAsBinaryString() 不支持 IE 10、11
FileReader.readAsBinaryString() is not support IE 10, 11
以下代码在 chrome 浏览器中有效。
$('#file').change(function(e) {
var fileReader = new FileReader(),
file = e.target.files[0];
if (typeof fileReader.readAsBinaryString == "function") {
// section #1
var binaryString, base64;
fileReader.onload = function(readerEvt) {
binaryString = readerEvt.target.result;
base64 = 'data:'+type+';base64,'+btoa(binaryString);
socket.emit('image', { image: base64, size: file.size, filename: file.name });
}
fileReader.readAsBinaryString(file);
}else{
// section #2 in IE 10, 11...
var binary = "", bytes = e.target.result, length = bytes.length;
for (var i=0; i<length; i++) {
binary += String.fromCharCode(bytes[i]);
}
// How can I control 'binary' variable for IE 10, 11
}
});
我想让它像 Google Chrome 一样工作。
请在第 2 节中实现源代码。
尽管 documentation 说 readAsBinaryString
函数已定义,但它仍然给出 Can't be resolved
和 function definition not found
错误。
您可以试试这个代码。它对我有用。请参考我的评论以获得帮助。
我已经在 IE 11 和 Chrome.
中测试了代码
Html代码
<html>
<head>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/xlsx/0.8.0/jszip.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/xlsx/0.8.0/xlsx.js"></script>
<script src="index.js"></script>
</head>
<body>
<input type="file" id="files" onchange="handleFileSelect(event)"/>
<output id="list"></output>
</body>
</html>
Javascript代码
//readAsBinaryString function is not defined in IE
//Adding the definition to the function prototype
if (!FileReader.prototype.readAsBinaryString) {
console.log('readAsBinaryString definition not found');
FileReader.prototype.readAsBinaryString = function (fileData) {
var binary = '';
var pk = 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++) {
var a = bytes[i];
var b = String.fromCharCode(a)
binary += b;
}
pk.content = binary;
$(pk).trigger('onload');
}
reader.readAsArrayBuffer(fileData);
}
}
function handleFileSelect(evt) {
console.log(evt);
var reader = new FileReader();
reader.onload = function(e){
if (reader.result)
reader.content = reader.result;
//In IE browser event object is null
var 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 need
console.log(evt.target.files[0]);
reader.readAsBinaryString(evt.target.files[0]);
}
function processWorkbook(workbook) {
console.log(workbook.Sheets['sheet_name']['excel_cell_name_to_be_accessed'].v);
//For example
console.log(workbook.Sheets['sheet1']['C2'].v);
//you can iterate through all the sheets
for(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
}
以下代码在 chrome 浏览器中有效。
$('#file').change(function(e) {
var fileReader = new FileReader(),
file = e.target.files[0];
if (typeof fileReader.readAsBinaryString == "function") {
// section #1
var binaryString, base64;
fileReader.onload = function(readerEvt) {
binaryString = readerEvt.target.result;
base64 = 'data:'+type+';base64,'+btoa(binaryString);
socket.emit('image', { image: base64, size: file.size, filename: file.name });
}
fileReader.readAsBinaryString(file);
}else{
// section #2 in IE 10, 11...
var binary = "", bytes = e.target.result, length = bytes.length;
for (var i=0; i<length; i++) {
binary += String.fromCharCode(bytes[i]);
}
// How can I control 'binary' variable for IE 10, 11
}
});
我想让它像 Google Chrome 一样工作。
请在第 2 节中实现源代码。
尽管 documentation 说 readAsBinaryString
函数已定义,但它仍然给出 Can't be resolved
和 function definition not found
错误。
您可以试试这个代码。它对我有用。请参考我的评论以获得帮助。
我已经在 IE 11 和 Chrome.
中测试了代码Html代码
<html>
<head>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/xlsx/0.8.0/jszip.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/xlsx/0.8.0/xlsx.js"></script>
<script src="index.js"></script>
</head>
<body>
<input type="file" id="files" onchange="handleFileSelect(event)"/>
<output id="list"></output>
</body>
</html>
Javascript代码
//readAsBinaryString function is not defined in IE
//Adding the definition to the function prototype
if (!FileReader.prototype.readAsBinaryString) {
console.log('readAsBinaryString definition not found');
FileReader.prototype.readAsBinaryString = function (fileData) {
var binary = '';
var pk = 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++) {
var a = bytes[i];
var b = String.fromCharCode(a)
binary += b;
}
pk.content = binary;
$(pk).trigger('onload');
}
reader.readAsArrayBuffer(fileData);
}
}
function handleFileSelect(evt) {
console.log(evt);
var reader = new FileReader();
reader.onload = function(e){
if (reader.result)
reader.content = reader.result;
//In IE browser event object is null
var 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 need
console.log(evt.target.files[0]);
reader.readAsBinaryString(evt.target.files[0]);
}
function processWorkbook(workbook) {
console.log(workbook.Sheets['sheet_name']['excel_cell_name_to_be_accessed'].v);
//For example
console.log(workbook.Sheets['sheet1']['C2'].v);
//you can iterate through all the sheets
for(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
}