0

I have to upload image using camera and gallery in Ionic 3. My REST backend is in php. I have to send image data in "multipart/form-data" without fileTransfer.upload() function. When i am using print_r($_FILES) in php it is showing black array.

1
  • Can anybody help me? please. Commented Jul 5, 2018 at 11:28

1 Answer 1

1
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);
        });
    });
}
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you so much for your valuable comment. can we do this without fileTransfer.upload(uri, uploadUrl, options) of this function like we do with <input type="file" />.
Thank you friend. I solved my issue by using base64.
let file = fileList[0]; let formData: FormData = new FormData(); formData.append('cv', file, file.name); return this._authhttp.uploadFile(url, formData); you can use code like this to submit form with file

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.