Preview multiple image before upload file in Angular

前端 未结 2 545
予麋鹿
予麋鹿 2020-12-16 03:40

How can I preview multiple images that I have selected before uploading them in Angular?

I have managed to do it but only with one image, even though I sele

2条回答
  •  遥遥无期
    2020-12-16 04:14

    As shown in this stackblitz, you can store the image URLs in an array and display them with ngFor:

    The array of URLs is filled in detectFiles:

    export class AppComponent {
    
      urls = new Array();
    
      detectFiles(event) {
        this.urls = [];
        let files = event.target.files;
        if (files) {
          for (let file of files) {
            let reader = new FileReader();
            reader.onload = (e: any) => {
              this.urls.push(e.target.result);
            }
            reader.readAsDataURL(file);
          }
        }
      }
    }
    

提交回复
热议问题