1

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

app.module.ts:

import { FileUploadModule } from 'ng2-file-upload/ng2-file-upload';
..
imports: [
        FileUploadModule
]

组件.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;
    }
}

app.component.html:

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

函数 fileOverBase 在拖动时成功调用,事件 e 打印为 true。现在我怎样才能得到拖动文件的对象?

4

2 回答 2

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;
  }
}
于 2017-06-07T05:55:14.290 回答
2

我知道它的回复晚了,但可能会对其他人有所帮助

更改 app.component.html: 带有 ** 代码

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

更改component.ts:如下**代码

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

**public onFileDrop(fileList: File[]) {
    console.log(fileList);// u get file as fileList[0]
}**
}
于 2017-08-02T12:41:18.540 回答