mobileUpload(): Observable<any> {
let uploadUrl = '<YOUR API ENDPOINT>';
const fileTransfer: FileTransferObject = this.transfer.create();
return Observable.create((observer: Observer<any>) => {
this.filepickerService.pick().subscribe(uri => {
let fileName = uri.substring(uri.lastIndexOf('/') + 1).split('?').reverse().pop();
let options: FileUploadOptions = {
fileKey: 'file',//key name you will use in $_FILE
fileName: fileName,
mimeType: this.filepickerService.mimeType(uri),
headers: {
"Authorization": "Bearer " + this._authService.getAccessToken()
}
}
let loader = this._loadingCtrl.create();
loader.present();
fileTransfer.upload(uri, uploadUrl, options)
.then((data) => {
loader.dismiss();
observer.next(JSON.parse(data.response));
observer.complete();
},
(err) => {
loader.dismiss();
observer.error(err);
}).catch(e => {
loader.dismiss();
});
}, err => {
observer.error(err);
});
});
}