首页 > 解决方案 > 如何使用后端 PHP 在 ionic 4/5 中上传图像?

问题描述

我想上传一张图片,为此,我的后端开发人员给了我邮递员的 JSON 演示,他要求将图片添加为 Multipart 表单数据。

首先,正如他所说的多部分表单数据,所以我像这个代码一样发送给他,但现在他说不是这个。

这是我的邮递员个人资料更新屏幕

在此处输入图像描述

上面的图像图像键头像和值是个人资料图片。请帮我...

标签: ionic-frameworkionic4

解决方案


async uploadImageData(formData: FormData) {

    const loading = await this.loadingController.create({

        message: 'Uploading image...',

    });

    await loading.present();

 

    this.http.post("http://localhost:8888/upload.php", formData)

        .pipe(

            finalize(() => {

                loading.dismiss();

            })

        )

        .subscribe(res => {

            if (res['success']) {

                this.presentToast('File upload complete.')

            } else {

                this.presentToast('File upload failed.')

            }

        });

}

这是上传代码,完整代码来自此参考: https ://devdactic.com/ionic-4-image-upload-storage/


推荐阅读