将图像文件发布到在 Ionic 3 中不起作用的事件 [英] Publishing image file to events not working in Ionic 3

查看:16
本文介绍了将图像文件发布到在 Ionic 3 中不起作用的事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

这是我的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,它作为文件 [对象] 发送.

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].

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

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

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

非常感谢任何帮助.

推荐答案

publish imageUrlevents 而不是 selectedImage.然后您可以在subscribeevents 的页面中显示图像.

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 演示

这篇关于将图像文件发布到在 Ionic 3 中不起作用的事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆