美文网首页
readAsBinaryString 兼容IE11

readAsBinaryString 兼容IE11

作者: littleStar_up | 来源:发表于2018-08-09 17:20 被阅读0次

    IE不支持html5 file api中readAsBinaryString,貌似还是ie10可以,ieEdge可以ie11不行,真是一个神奇的存在
    然后找了个方法(轮子是造不出了只能搬砖了),大神重写了readAsBinaryString

    FileReader.prototype.readAsBinaryString = function (fileData) {
    var binary = "";
    var pt = 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++) {
            binary += String.fromCharCode(bytes[i]);
        }
        pt.content = binary;
        pt.onload(pt); //页面内data取pt.content文件内容
      }
      reader.readAsArrayBuffer(fileData);
    }
    

    下面是页面读取excel

    doUpload() {
      if(!$("#file").get(0).files[0]) {
          return;
      }
      var file = $("#file").get(0).files[0];
      var reader = new FileReader();
      var strArr = [];
      var wb;
      var rABS = false;
      let that = this;
      reader.onload = function(data) {
      var data = data.content;
      if(rABS) {
        wb = XLSX.read(btoa(this.fixdata(data)), { //手动转化
            type: 'base64'
        });
      } else {
        wb = XLSX.read(data, {
        type: 'binary'
        });
      }
      //wb.SheetNames[0]是获取Sheets中第一个Sheet的名字
      //wb.Sheets[Sheet名]获取第一个Sheet的数据
      var tempArr = XLSX.utils.sheet_to_json(wb.Sheets[wb.SheetNames[0]]);
      for(var i = 0; i < tempArr.length; i++) {
          var tt = tempArr[i];
          var ss = {};   
          ss.filename = tt['文件名称'];
          ss.filenum = tt['张数'];
          ss.remark = tt['备注'];
          ss.editable = false;
          strArr.push(ss);
          that.form.qdmxVOS.push(ss);
      }
      that.dialogVisible = false;
      that.page = 'view';
      };
      if(rABS) {
          reader.readAsArrayBuffer(file);
      } else {
          reader.readAsBinaryString(file);
      }
    

    }

    相关文章

      网友评论

          本文标题:readAsBinaryString 兼容IE11

          本文链接:https://www.haomeiwen.com/subject/poecbftx.html