将库中选择的base64图像发送到服务器Ionic3

时间:2019-07-27 12:57:42

标签: ionic3 angular5

嗨,我正在使用相机和图像选择器插件来添加使用相机和图库添加照片的功能。但是当我从图库中选择图像并尝试发送到服务器时,作为base64图像,我无法将其获取到服务器端。它发送空字符串。

func commonInit(){
      let customView = Bundle.main.loadNibNamed("customCellView", owner: self, options: nil)?.first as? UIView ?? UIView()
      customView.frame = self.bounds
      self.addSubview(customView)

      customView.button.addTarget(self, action: #selector(actionCustom), for: .touchUpInside) //Add click code here
  }

所以我在这里发送this.pic = base64File;像这样向服务器发送发布请求的服务。

add.dog.ts.
//Open image picker multiple images
  openImagePicker(){
let options = {
  maximumImagesCount: 3,
}
this.photos = new Array<string>();
this.imagePicker.getPictures(options)
.then((results) => {
  this.reduceImages(results).then(() => {        
    for (let index = 0; index < results.length; index++) {          
        this.photos.push(results[index]);  
        this.base64.encodeFile(results[index]).then((base64File: string) => {
        this.pic = base64File;
        this.authService.uploadPhotosServer(this.pic).then((result) => {
        this.responseData = result;  
  }, (err) => {
          console.log(err);
        });

      }
    //console.log("Image Lists", this.photos);
  });
}, (err) => { console.log(err) });

 }

在服务器端,我正在尝试访问数据。

  uploadPhotosServer(photos){ //console.log(photos);
   return new Promise((resolve, reject) => {
    let headers = new Headers();
    let datafile = photos;
     this.http.post(uploadDogPhotosGalleryAuthEndPoint,datafile, {headers: headers})
  .subscribe(res => {
    resolve(res.json());
  }, (err) => {
    resolve(err.json());
  });
 });
}

但是其打印始终为空。如果我检查控制台,则其显示请求有效负载之类。

if ($_SERVER['REQUEST_METHOD'] == 'POST') {        
    echo "<pre>";print_r($_REQUEST);die;
  }

可能是什么问题,请帮忙

谢谢

1 个答案:

答案 0 :(得分:1)

尝试一下:

$postData = file_get_contents('php://input');
var_dump($postData )