读取IE11中的readAsBinaryString

时间:2018-09-03 09:31:19

标签: angular typescript angular-cli

尝试在IE10 / 11中将二进制读取为字符串,但失败。在Chrome中工作。 尝试过不同的IE修复程序,但没有成功。我似乎最大的问题是我找不到结果。它给出了“ angular / cli”错误:“ 1.6.5”。

  

e.target.result返回null,在编译器中不接受

         FileReader.prototype.readAsBinaryString = function(blb) {
      const that = this;
      const conversor = function (e) {
          const toConvert = e.target.result || '';
          let binary = '';
          const bytes = new Uint8Array(toConvert);
  

reader.result返回null,在编译器中不接受

  if (FileReader.prototype.readAsBinaryString === undefined) {
      FileReader.prototype.readAsBinaryString = function (fileData) {
        let binary = '';
        const pt = this;
        reader.onload = function (e) {
          const bytes = new Uint8Array(reader.result);
          const length = bytes.byteLength;
          for (let i = 0; i < length; i++) {
            binary += String.fromCharCode(bytes[i]);
          }
          const fl = {target: {result: binary}};
          pt.onload(fl);
        };
        reader.readAsArrayBuffer(fileData);
      };
    }

尝试解析它,编译器喜欢它,但是不起作用。

     const r: any = reader;
      const t: any = e.target;
      let data;
      if (!e) {
          data = r.content;
       } else {
         data = t.result;
      }

有什么想法吗?

JavaScript readAsBinaryString Function on E11

1 个答案:

答案 0 :(得分:1)

我通过反复尝试找到了适用于Chrome和IE的组合解决方案。

  this.sharepointService.getFile(this.sharepointService.siteUrl + 'Templates/' + template.fileLeafRef).subscribe(f => {
    const reader = new FileReader();
    let binary = '';
    if (FileReader.prototype.readAsBinaryString === undefined) {
      FileReader.prototype.readAsBinaryString = function (fileData) {
        const pt = this;
        reader.onload = function (e) {
          let  ia = null;
          let length = 0;
          if (e) {
            ia = new Uint8Array(e.target['result']);
            length = ia.length;
          }
          for (let i = 0; i < length; i++) {
            binary += String.fromCharCode(ia[i]);
          }
        };

       reader.readAsArrayBuffer(fileData);
      };
    }

    reader.readAsBinaryString(f);

    reader.onloadend = () => {

        let base64data;
        if ( binary === '') {
          base64data = reader.result;
        } else {
          base64data = binary.toString();
        }