Converting an image to base64 in angular 2

后端 未结 7 568
醉话见心
醉话见心 2020-11-30 05:31

Converting an image to base64 in angular 2, image is uploaded from local . Current am using fileLoadedEvent.target.result. The problem is, when I send this base64 string thr

7条回答
  •  我在风中等你
    2020-11-30 05:49

    I modified Parth Ghiya answer a bit, so you can upload 1- many images, and they are all stored in an array as base64 encoded strings

    base64textString = [];
    
    onUploadChange(evt: any) {
      const file = evt.target.files[0];
    
      if (file) {
        const reader = new FileReader();
    
        reader.onload = this.handleReaderLoaded.bind(this);
        reader.readAsBinaryString(file);
      }
    }
    
    handleReaderLoaded(e) {
      this.base64textString.push('data:image/png;base64,' + btoa(e.target.result));
    }
    

    HTML file

    
    
    

提交回复
热议问题