在Angular 6中使用ngx-file-drop上传之前如何预览图像?

时间:2018-09-15 16:28:46

标签: angular file-upload angular6 ng-file-upload

我正在使用“文件拖放”标签在Angular 6上拖放图像。

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

我从(onFileDrop)="dropped($event)"获得了文件[0],但是我不知道如何在将该图像上传到服务器之前显示该图像,在此先感谢:/

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

注意:我正在尝试实现以下方法,但是可以找到event.target?来自(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 个答案:

答案 0 :(得分:1)

fileEntry是FileSystemEntry的实例,但是如果您上传文件而不是文件夹,则fileEntry实际上是FileSystemFileEntry实例。这意味着它具有file()方法,您可以这样做:

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

答案 1 :(得分:0)

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