首页 > 解决方案 > 在 ionic 3 中上传图像数组

问题描述

我可以使用此功能将图像发送到服务器,但我还不知道如何发送这些图像的数组,我正在使用 cordova 插件获取图像。

getPicture(){
        const options: CameraOptions = {
            quality: 100,
            destinationType: this.camera.DestinationType.DATA_URL,
            encodingType: this.camera.EncodingType.JPEG,
            sourceType: this.camera.PictureSourceType.CAMERA,
            saveToPhotoAlbum: true
        }

        this.camera.getPicture(options).then((imageData) => {
            // imageData is either a base64 encoded string or a file URI
            // If it's base64 (DATA_URL):
            this.cameraData = imageData;
            this.base64Image = "data:image/jpeg;base64," + imageData;
            this.photos.push(this.base64Image);
            this.photos.reverse();


        }, (err) => {
            // Handle error
        });
    }

我用这个发送到服务器

onSubmit() {
      let loading = this.loadingCtrl.create({
          content: 'Carregando...',
      });
      loading.present();

      let url = "backend_cliente/api/api_upload_image.php";
      let postData = new FormData();
      postData.append('id', this.id);
      postData.append('file', this.base64Image);
      let data: Observable<any> = this.http.post(url, postData);
      data.subscribe((result) => {
          loading.dismissAll();
          alert(result);
      }, error1 => {
          loading.dismissAll();
          let mensagem = "Upload success";
          this.presentAlert(mensagem);
      });

  }

我尝试了几次,但我找不到发送这些图像数组的方法,因为我使用的函数不发送字符串数组。

并使用此后端接收货物。

$data = ['result' => false];
$id = isset($_POST['id'])? $_POST['id'] : '';
$path = 'uploads/user-id-'.$id;
if(!file_exists($path )){
    mkdir("$path ", 0700);
}

$target_path = $path .'/'.$id.'.jpg';
$return = '';
if (isset($_POST['file'])) {

    $imageData = $_POST['file'];
    $imageData = str_replace('data:image/jpeg;base64,', '',$imageData);
    $imageData = str_replace('data:image/jpg;base64,', '',$imageData);
    $imageData = str_replace(' ', '+', $imageData);
    $imageData = base64_decode($imageData);

    file_put_contents($target_path, $imageData);   

}

header("Access-Control-Allow-Origin: *"); 
header("Content-Type: application/json");

如果有更好的功能来发送数组,请告诉我

标签: phpionic3

解决方案


推荐阅读