3

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();    }
4
  • Your last note seems to be a not fully clear for me. Commented Sep 15, 2018 at 17:30
  • // I'm trying to implement like a (change)="myFuc($event)" but it fails for (onFileDrop)="dropped($event)" and get error as event.target or event.target.files[0] is null Commented Sep 15, 2018 at 18:19
  • I just want to show the image file that gets from (onFileDrop)="dropped($event)" before it uploads to server Commented Sep 15, 2018 at 18:20
  • Can you provide an example on stackblitz Commented Sep 16, 2018 at 13:34

2 Answers 2

4

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;
            };
        });
    }
Sign up to request clarification or add additional context in comments.

Comments

1

<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;
     };
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.