IE 11 - 解决不了“对象不支持属性或方法‘charCodeAt’”或“对象不支持属性或方法‘readAsBinaryString’”

问题描述 投票:0回答:1

我使用xlsx.core.min.js上传Excel文件到SP及其内容传递到一个新的列表。我有它的边缘工作,但它不会在IE 11的工作。

我收到的第一个控制台错误是“对象不支持属性或方法‘charCodeAt’”

我读到的兼容性问题.readAsArrayBuffer(),并取代了它。然后,我开始收到错误“对象不支持属性或方法‘readAsBinaryString’”

**注:使用仅.readAsArrayBuffer()导致了原“charCodeAt”控制台错误**

我怎样才能得到这个代码的工作?先感谢您!

function ExportToTable() {
            var regex = /^([a-zA-Z0-9\s_\\.\-:])+(.xlsx|.xls)$/;
            /*Checks whether the file is a valid excel file*/
            if (regex.test($("#excelfile").val().toLowerCase())) {
                var xlsxflag = false; 
     /*Flag for checking whether excel is .xls format or .xlsx format*/
            if ($("#excelfile").val().toLowerCase().indexOf(".xlsx") > 0) {
            xlsxflag = true;
        }
        /*Checks whether the browser supports HTML5*/
        if (typeof (FileReader) != "undefined") {
            var reader = new FileReader();
            reader.onload = function (e) {
                var data = e.target.result;
                /*Converts the excel data in to object*/
                if (xlsxflag) {
                    var workbook = XLSX.read(data, { type: 'binary' });
                }
                else {
                    var workbook = XLS.read(data, { type: 'binary' });
                }
                /*Gets all the sheetnames of excel in to a variable*/
                var sheet_name_list = workbook.SheetNames;

                var cnt = 0; /*This is used for restricting the script to consider only first sheet of excel*/
                sheet_name_list.forEach(function (y) { /*Iterate through all sheets*/
                    /*Convert the cell value to Json*/
                    if (xlsxflag) {
                        var exceljson = XLSX.utils.sheet_to_json(workbook.Sheets[y]);
                    }
                    else {
                        var exceljson = XLS.utils.sheet_to_row_object_array(workbook.Sheets[y]);
                    }
                    //alert (exceljson.length + " ----" + cnt);
                    if (exceljson.length > 0 && cnt == 0) {
                        BindTable(exceljson, '#exceltable');
                        cnt++;
                    }
                });
                $('#exceltable').show();
            }
            if (xlsxflag) {/*If excel file is .xlsx extension than creates a Array Buffer from excel*/
                reader.readAsArrayBuffer($("#excelfile")[0].files[0]);
            }
            else {
                reader.readAsBinaryString($("#excelfile")[0].files[0]);
            }
        }
        else {
           // alert("Sorry! Your browser does not support HTML5!");
       }
    }
    else {
       // alert("Please upload a valid Excel file!");
    }

}
internet-explorer binary cross-browser blob compatibility
1个回答
0
投票

尝试参考下面的代码读取.xlsx文件。

document.getElementById('file-object').addEventListener("change", function(e) {
  var files = e.target.files,file;
  if (!files || files.length == 0) return;
  file = files[0];
  var fileReader = new FileReader();
  fileReader.onload = function (e) {
    var filename = file.name;
    // pre-process data
    var binary = "";
    var bytes = new Uint8Array(e.target.result);
    var length = bytes.byteLength;
    for (var i = 0; i < length; i++) {
      binary += String.fromCharCode(bytes[i]);
    }
    // call 'xlsx' to read the file
    var oFile = XLSX.read(binary, {type: 'binary', cellDates:true, cellStyles:true});
  };
  fileReader.readAsArrayBuffer(file);
});

更多详细信息,请this article

© www.soinside.com 2019 - 2024. All rights reserved.