src / app / app.component.ts(20,27)中的错误:错误TS2345:“字符串|类型”的参数ArrayBuffer'不能分配给'string'类型的参数

时间:2019-09-11 05:10:47

标签: angular

  

`src / app / app.component.ts(20,27)中的错误:错误TS2345:“字符串|类型”的参数ArrayBuffer'不可分配给'string'类型的参数。         不能将“ ArrayBuffer”类型分配给“字符串”类型。

        import { Component } from '@angular/core';
        declare const fileSelected: any;

         @Component({
       selector: 'app-root',
      templateUrl: './app.component.html',
      styleUrls: ['./app.component.css']
      })
      export class AppComponent {

     binaryStr :any;
     inputObj :any;
      fileSelected(inputObj){

        var fileObj = this.inputObj.files[0];
        var webworkerReader = new FileReader();

        webworkerReader.onload = function(){
       var binaryStr = webworkerReader.result;
       var base64Str = btoa(binaryStr);

        webworkerReader.readAsBinaryString(fileObj);
       };
      }
       onclick(){
       fileSelected(this.inputObj);

      }
   }

为什么会出现此错误?如何解决?

2 个答案:

答案 0 :(得分:0)

只需在此处使用angular变量,而不要创建var变量

 var binaryStr = webworkerReader.result; 

this.binaryStr = webworkerReader.result; 

答案 1 :(得分:0)

您可以只使用FileReader.result

例如,

const fileReader: FileReader = new FileReader();

fileReader.onload = (event: Event) => {
   event.target.result; // This is invalid
   fileReader.result; // This is valid
};

更改:var webworkerReader : FileReader = new FileReader();