我想在文件拖拽时完成上传:我使用的是ng2- file -upload 1.2.1版本,代码片段如下:
app.module.ts:
import { FileUploadModule } from 'ng2-file-upload/ng2-file-upload';
..
imports: [
FileUploadModule
]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;
}
}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。现在,我如何才能获得被拖动文件的对象??
发布于 2017-06-07 13:55:14
您需要使用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-08-02 20:41:19
我知道它的回复很晚,但希望它能对其他人有所帮助
更改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]
}**
}https://stackoverflow.com/questions/44044254
复制相似问题