在ionic 3中上传图像数组

时间:2019-01-14 16:15:29

标签: php ionic3

我可以使用此功能将图像发送到服务器,但是我还不知道如何发送这些图像的数组,我正在使用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");

如果有更好的函数发送数组,请告诉我

0 个答案:

没有答案