将图像文件发布到Ionic 3中不起作用的事件

问题描述 投票:1回答:1

我在Ionic App工作,我在我的应用程序中制作了更新配置文件图像功能,它工作正常但是当我尝试更新用户配置文件图像时,它不会发送图像正确的路径。

这是我的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);
            }
          }
        });
      }
    });
  }

在我的ts文件中,我将图像发送到我的app.html,使用以下代码显示更新的图像:this.events.publish('userprofile:created', this.selectedImage);但问题是它没有发送它作为文件[对象]发送的正确图像URL。

供参考:https://stackblitz.com/edit/ionic-ydphaq

当我将图像发送到我的about.ts时,它显示错误。

任何帮助深表感谢。

ionic-framework ionic3
1个回答
2
投票

publish imageUrl到events不是selectedImage。然后你可以在subscribeevents的页面中显示图像。

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

StackBlitz Demo

© www.soinside.com 2019 - 2024. All rights reserved.