I want to accomplish upload on file drag: I am using ng2-file-upload version 1.2.1 with following code snippet:
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>
The function fileOverBase gets successfully called on drag, with event e printed as true. Now How can i get the dragged file's object??
You need to use the afterAddingfile method to get the file object in the ng2-file-upload plugin.
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;
}
}
I know its late reply but may it will help to others
change app.component.html: with ** code
<div class="well" ng2FileDrop [uploader]="uploader" [ngClass]="{'another-file-over-class': hasBaseDropZoneOver}"
(fileOver)="fileOverBase($event)" **(onFileDrop)="onFileDrop($event)"**
>
Drop CSV here
</div>
change component.ts: as below ** code
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]
}**
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.