1

I have a requirement where I need to convert the selected image (a part of a form group) to a byte array which needs to be sent as a part of a post request to the backend.

HTML Component:

<div class="form-group">
                <label>Image</label>
                <input type="file" accept="image/*" class="form-control" formControlName="productImage" onchange="displaySelectedImageFunc(this);">
                <img *ngIf="displaySelectedImage" src="{{selectedImageUrl}}">
                <span class="error-message" *ngIf="itemRegistrationForm.controls.productImage.dirty && itemRegistrationForm.controls.productImage.errors">Select Image of the product</span>
</div>
Younes
  • 462
  • 7
  • 15
Ani
  • 65
  • 1
  • 2
  • 11
  • Can you try this one? https://stackoverflow.com/questions/53390580/convert-byte-array-to-image-in-angular6 – Bhavin Feb 03 '21 at 09:16
  • @Bhavin Hi, I need to do the opposite which is to convert the image to byte array – Ani Feb 03 '21 at 09:24
  • They have used "btoa", you can try passing your base 64 string to "atob" ref: https://stackoverflow.com/questions/28185781/how-to-convert-image-into-byte-array-in-jsp-javascript – Bhavin Feb 03 '21 at 09:30
  • i found a npm package which can be useful https://www.npmjs.com/package/image-to-base64. Conversion from base64 to byte array should be easier https://stackoverflow.com/questions/42360999/convert-base64-string-into-byte-array-through-javascript – Dawid .T Feb 03 '21 at 09:30
  • You can use FileReader.readAsArrayBuffer() – raymondboswel Feb 03 '21 at 09:42
  • Thank you all the given solution works. – Ani Feb 03 '21 at 16:03

2 Answers2

4

Try Using

function convertDataURIToBinary(dataURI) {
  var base64Index = dataURI.indexOf(';base64,') + ';base64,'.length;
  var base64 = dataURI.substring(base64Index);
  var raw = window.atob(base64);
  var rawLength = raw.length;
  var array = new Uint8Array(new ArrayBuffer(rawLength));

  for(i = 0; i < rawLength; i++) {
    array[i] = raw.charCodeAt(i);
  }
  return array;
}

function upload() {
  const file = document.querySelector('input[type=file]').files[0];
  
  const preview = document.getElementById('preview'); 
  const reader = new FileReader();
  let byteArray;

  reader.addEventListener("loadend", function () {
    // convert image file to base64 string
    console.log('base64', reader.result);
    preview.src = reader.result;
    byteArray = convertDataURIToBinary(reader.result);
    console.log('byte array', byteArray);
  }, false);

  if (file) {
    reader.readAsDataURL(file);
  }
}
<input type="file" id="file" accept="image/*" width="300" height="300" />

<submit onClick="upload()">Upload</submit>

<img id="preview"></img>
Msk Satheesh
  • 1,398
  • 1
  • 6
  • 7
1

Since you can convert the image to base64 string using reader.readAsDataUR(file) you can extract the byte part with e.target.result.split('base64,')[1] on the reader.onload event like this

const file = document.querySelector('input[type=file]').files[0])
const reader = new FileReader();
reader.onload = (e: any) => {
  const bytes = e.target.result.split('base64,')[1];
};
reader.readAsDataURL(file);
daniel rubambura
  • 545
  • 6
  • 12