vendredi 10 août 2018

Ionic formData append showing null in server

I am trying to upload an image using formData. The api is working fine. But the data is displaying null in the server. My function is

  capture_dl_front(){
    this.camera.getPicture(this.cameraOptions)
      .then(imageData => {
        this.customer.dl_front = normalizeURL(imageData);
        this.upload_dl_front(imageData);
      }, error => {
        this.func.showAlert('Error',JSON.stringify(error));
      });
  }
  upload_dl_front(imageFileUri: any): void {
    this.file.resolveLocalFilesystemUrl(imageFileUri)
      .then(entry => (<FileEntry>entry).file(file => this.readFile_dl_front(file)))
      .catch(err => console.log('Error',JSON.stringify(err)));
  }
  private readFile_dl_front(file: any) {
    const reader = new FileReader();
    reader.onloadend = () => {
      const imgBlob = new Blob([reader.result], { type: file.type });
      this.dl_front_imageUri = imgBlob;
      this.dl_front_imageName = file.name;
      alert(this.dl_front_imageName)
      const img = new FormData();
      img.append('image', this.dl_front_imageUri, this.dl_front_imageName)
      this.api.test(img).then(data=>alert("final: "+data))
    };
    reader.readAsArrayBuffer(file);
  }

and my api function is

 test(image){
    let headers = new HttpHeaders({
      'Content-Type': 'application/x-www-form-urlencoded',
    });
    return new Promise( resolve => {
      this.http.post(url, image, { headers: headers})
        .subscribe(
          data => {
            resolve(data['message']);
          },
          error => {
            resolve(error.statusText);
          }
        );
    });
  }

and i am getting the file in my laravel server as

$image = $request->file('image');

but i am getting null in the image parameter.

What am i doing wrong here?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire