Publishing image file to events not working in Ion

2020-02-15 05:35发布

问题:

I am working in Ionic App and I have made the update profile image functionality in my app and it is working fine but when I try to update the user profile image it is not sending the image proper path.

This is my updateimage.ts:

onImageSelected(event) {
    this.selectedImage = event.target.files[0];
    let reader = new FileReader();

    reader.onload = (e: any) => {
      this.imageUrl = e.target.result;
      this.converted_image = "data:image/jpeg;base64,"+this.imageUrl;
    };
    reader.readAsDataURL(this.selectedImage);
  }

  changeProfileImage()
  { 
    this.storage.get("ID").then((val) =>
    {
      if(val)
      { 
        var fd = new FormData();
        fd.append('upic', this.selectedImage, this.selectedImage.name);
        fd.append('user_id', val);
        this.restProvider.updateprofileimg(fd, 'update_profilepic/'+val).subscribe((data) => {
          if (data) {
            this.responseEdit = data;
            if (this.responseEdit.status === 'success') {
              this.events.publish('userprofile:created', this.selectedImage); <!-- I am sending the image to the app.html -->
              this.presentAlert(this.responseEdit.msg);
            }
          }
        });
      }
    });
  }

In my ts file, I am sending the image to my app.html of showing the updated image using this code: this.events.publish('userprofile:created', this.selectedImage); but the problem is that it is not sending the proper image URL it is sending as a file[object].

For Reference: https://stackblitz.com/edit/ionic-ydphaq

When I am sending the image to my about.ts, it is showing the error.

Any help is much appreciated.

回答1:

You publish imageUrl to events not the selectedImage. Then you can show image in the pages where subscribe to events.

this.events.publish('userprofile:created', this.imageUrl);

StackBlitz Demo