Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Picture upload in Ionic 4 with camera and file plugins

I am trying to upload file in Ionic 4 to the server but getting the error 2nd parameter must be blob.

But I have logged the blob image it is coming properly.

I am following in this https://devdactic.com/ionic-4-image-upload-storage/ this link for the guide.

I don't need to save this to local storage so I have skipped that section.

selectImageInGallery() {
    const options: CameraOptions = {
      quality: 100,
      sourceType: this.camera.PictureSourceType.PHOTOLIBRARY,
      destinationType: this.camera.DestinationType.FILE_URI,
      encodingType: this.camera.EncodingType.JPEG,
      mediaType: this.camera.MediaType.PICTURE,
      saveToPhotoAlbum: false
    };
    this.camera.getPicture(options).then(
      imageData => {
        const formData = this.uploadAvatar(imageData);
        this.uploadAvatar(formData);
      },
      err => {
        this.toastService.presentToastBottom('Failed to select the image');
      }
    );
  }

uploadAvatar(imagePath: any) {
    let fileName = '';
    this.file
      .resolveLocalFilesystemUrl(imagePath)
      .then(fileEntry => {
        const { name, nativeURL } = fileEntry;
        const path = nativeURL.substring(0, nativeURL.lastIndexOf('/'));
        fileName = name;
        return this.file.readAsArrayBuffer(path, name);
      })
      .then(buffer => {
        const imgBlob = new Blob([buffer], {
          type: 'image/jpeg'
        });
        console.log(imgBlob);
        const formData: FormData = new FormData();
        formData.append('avatar', imgBlob);
        this.userService.updateAvatar(formData).then(res => {
          console.log(res);
          this.loader.hideLoader();
          // if (res.data.status === 'success') {
          //   this.user = res.data.user;
          // } else if (res.data.status === 'error') {
          //   this.toastService.presentToast(res.data.message);
          // } else {
          //   this.toastService.presentToast('Server Error');
          // }
        });
      });
  }

Service
public async updateAvatar(postData: any) {
    const httpOptions = {
      headers: new HttpHeaders({
        'Content-Type': 'multipart/form-data',
        'X-Authorization': environment.appKey,
        Authorization: localStorage.getItem('TOKEN_KEY')
      })
    };
    const res = await this.http
      .post(
        environment.apiUrl + 'user/update-profile-picture',
        postData,
        httpOptions
      )
      .toPromise();
    return res as any;
  }
like image 615
Arindam Pal Avatar asked Dec 05 '25 08:12

Arindam Pal


1 Answers

How closely have you followed this tutorial?

As I recall from watching Simon's videos (devdactic author), there are complications working with images where you need to create at least a temporary file on the system to send off to other places.

First, you should follow the tutorial exactly and then once it is working you can experiment with removing things.

In the first section of the tutorial it clearly states:

  • Upload files from their local path using a POST request

So I don't think just removing chunks of the tutorial is your best technique for getting this going.

Learn from the expert first, modify later.

like image 124
rtpHarry Avatar answered Dec 08 '25 19:12

rtpHarry



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!