Reputation: 55
I am trying to use bulk upload in angular2,i googled about it but still i am not clear about that .Can anyone please post me help.Thanks.
Upvotes: 0
Views: 1236
Reputation: 4993
try this code implementation
<input type="file" (change)="fileChange($event)" placeholder="Upload files" multiple>
fileChange(event) {
let fileList: FileList = event.target.files;
let fileListLength = fileList.length;
if(fileListLength > 0) {
let formData:FormData = new FormData();
for (var i = 0; i < fileListLength; i++) {
formData.append("uploadFile[]", fileList[i]);
}
let headers = new Headers();
headers.append('Content-Type', 'multipart/form-data');
headers.append('Accept', 'application/json');
let options = new RequestOptions({ headers: headers });
this.http.post(`${this.apiEndPoint}`, formData, options)
.map(res => res.json())
.catch(error => Observable.throw(error))
.subscribe(
data => console.log('success'),
error => console.log(error)
)
}
}
Upvotes: 1