how to preview image before upload using ngx-file-drop in angular 6?

Viewed 5155

I'm using "file-drop" tag to drag and drop image on Angular 6.

 <file-drop headertext="{{adCrud.uploadMediaText}}"
    (onFileDrop)="dropped($event)" (onFileOver)="fileOver($event)"
    customstyle="filedrop" customstyle="adMedia"
    (onFileLeave)="fileLeave($event)" >

I got the file[0] from (onFileDrop)="dropped($event)", but I've no idea how to show that image before upload it to the server, thanks in advance :/

   ---------------

Note: I'm trying to implement the below method but could find event.target ? from (onFileDrop)="dropped($event)"

media1change(event) {
    if (event.target.files && event.target.files[0]) {
      const reader = new FileReader();

      // tslint:disable-next-line:no-shadowed-variable
      reader.onload = (event: ProgressEvent) => {
        this.adCrud.media1 = (<FileReader>event.target).result;
        // // console.log(this.profile);
        // this.uploadmedia1();
      };

      reader.readAsDataURL(event.target.files[0]);
    }
    this.media1File = event.target.files[0];
    this.uploadmedia1();    }
2 Answers

The fileEntry is an instance of FileSystemEntry, but if you upload a file and not a folder, the fileEntry is actually a FileSystemFileEntry instance. This means it has the file() method and you can do it like so:

imageDropped($event: UploadEvent) {
        const droppedFile = $event.files[0];
        const fileEntry = droppedFile.fileEntry as FileSystemFileEntry;
        const reader = new FileReader();

        fileEntry.file(file => {
            reader.readAsDataURL(file);
            reader.onload = () => {
                this.imageUrl = reader.result;
            };
        });
    }

<img [src]="**imageUrlPREVIEW**">

imageUrlPREVIEW;
...
const droppedFile = event.files[0];
const fileEntry = droppedFile.fileEntry as FileSystemFileEntry;
const reader = new FileREader();
fileEntry.file((file: File) => {
     reader.readASDAtaURL(file);
     reader.onload = () => {
         **this.imageUrlPREVIEW**= reader.result;
     };
});
Related