Publishing image file to events not working in Ionic 3

梦想的初衷 提交于 2019-12-11 16:55:41

问题


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



来源:https://stackoverflow.com/questions/54760515/how-to-show-the-updated-profile-image-without-reloading-the-ionic-app

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!