如何使用ng2-file-upload + angular 2获取文件对象

cli*_*int 1 angular-file-upload angular2-directives ng2-file-upload angular

我想完成上传文件拖动:我正在使用ng2-file-upload版本1.2.1以及以下代码片段:

app.module.ts:

import { FileUploadModule } from 'ng2-file-upload/ng2-file-upload';
..
imports: [
        FileUploadModule
]
Run Code Online (Sandbox Code Playgroud)

component.ts:

import { FileUploader } from 'ng2-file-upload/ng2-file-upload';

...

class AppXYZComponent{
private uploader: FileUploader = new FileUploader({ url: 'blah.com' });

    public hasBaseDropZoneOver:boolean = false;
    //public hasAnotherDropZoneOver:boolean = false;

    public fileOverBase(e:any):void {
        console.log("hasBaseDropZoneOver", e);
        this.hasBaseDropZoneOver = e;
    }
}
Run Code Online (Sandbox Code Playgroud)

app.component.html:

<div class="well" ng2FileDrop [uploader]="uploader" [ngClass]="{'another-file-over-class': hasBaseDropZoneOver}"
             (fileOver)="fileOverBase($event)"
             >
            Drop CSV here
        </div>
Run Code Online (Sandbox Code Playgroud)

函数fileOverBase在拖动时成功调用,事件e打印为true.现在我如何获得拖动文件的对象?

Sat*_*tha 6

您需要使用afterAddingfile方法来获取ng2-file-upload插件中的文件对象.

import { FileUploader } from 'ng2-file-upload/ng2-file-upload';
...

class AppXYZComponent{
public uploader: FileUploader;
public hasBaseDropZoneOver:boolean = false;
//public hasAnotherDropZoneOver:boolean = false;

constructor(){
   this.uploader = new FileUploader({ url: 'blah.com' });
   this.uploader.onAfterAddingFile = (fileItem) => {
                fileItem.withCredentials = false;
                console.log(fileItem); // fileItem is the file object
            };
}
 public fileOverBase(e:any):void {
    console.log("hasBaseDropZoneOver", e);
    this.hasBaseDropZoneOver = e;
  }
}
Run Code Online (Sandbox Code Playgroud)