Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
371 views
in Technique[技术] by (71.8m points)

ionic3 - Publishing image file to events not working in Ionic 3

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...