Angular 2 encode image to base64

后端 未结 4 1547
甜味超标
甜味超标 2020-12-24 02:28

I want to encode the uploaded files to base64 so that I can pass them to the request. The problem is that I\'m using Angular 2 with Typescript and I couldn\'t find any info

4条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-24 02:36

    So I find the solution:

    compontent.ts

    changeListener($event) : void {
      this.readThis($event.target);
    }
    
    readThis(inputValue: any): void {
      var file:File = inputValue.files[0];
      var myReader:FileReader = new FileReader();
    
      myReader.onloadend = (e) => {
        this.image = myReader.result;
      }
      myReader.readAsDataURL(file);
    }
    

    component.html

    
    

提交回复
热议问题