Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Cannot read properties of undefined reading base64 – Angular

I have input field to upload image

<div class="form-group">
  <label for="exampleFormControlInput1">
    Upload Image
  </label>
  <input #imageInput type="file" accept='image/*' class="form-control" (change)="onChange($event)"
  />
</div>
<div class="pb-4 form-group float-right">
  <button class="btn btn-primary btn-main" (click)="saveImage()">
    Save
  </button>
  &nbsp;
  <button class="btn btn-primary btn-cancel">
    Cancel
  </button>
</div>

Here’s where i catch event when file is uploaded then save.

onChange(event) {
  this.imageChangedEvent = event;
}

saveImage() {
  console.log(this.selectedPicture) this.apiService.postData('educational-award/', {
    "photo": this.selectedPicture.base64,
    "id": this.educationalData.id
  }).subscribe(data = >{
    console.log('test')
  });
}

But when i console.log(this.selectedPicture) it gives me undefined

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

enter image description here

>Solution :

You can try this:

onChange(event) {
    this.imageChangedEvent = event.target.files[0];
  }

  saveImage() {
    const file = this.imageChangedEvent;
    const reader = new FileReader();
    reader.readAsDataURL(file);
    reader.onload = () => {
      this.apiService.postData('educational-award/', {
        "photo": reader.result,
        "id": this.educationalData.id
      }).subscribe(data => {
        console.log('test')
      });
    };
  }
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading