Image preview before upload in angular 5

后端 未结 7 1690
逝去的感伤
逝去的感伤 2020-12-15 10:17

.html

  
  \"your
相关标签:
7条回答
  • 2020-12-15 10:51

    What about using @HostListner, since Angular doesn’t come with a built-in value accessor for file input.

      @HostListener('change', ['$event.target.files'])
      emitFiles( event: FileList ) {
        const file = event && event.item(0);
        this.onChange(file);
        const reader = new FileReader();
        reader.readAsDataURL(file); // toBase64
        reader.onload = () => {
          this.imageURL = reader.result as string; // base64 Image src
        };
    

    Then in the HTML, you may use something like:

      <picture >
        <source media='(min-width:0px)' [srcset]="imageURL">
        <img src=""  [alt]="Your photo">
      </picture>
    
    0 讨论(0)
提交回复
热议问题