Skip to content Skip to sidebar Skip to footer

Converting An Image To Binary In Javascript Using Base64

I have to convert an image to binary for storing it through IPFS and retrieve it again as a viewable image. I should do this with javascript code. Does any body have any clear exam

Solution 1:

Use File Reader:

/******************for base 64 *****************************/
function uploadFile(inputElement) {
  var file = inputElement.files[0];
  var reader = new FileReader();
  reader.onloadend = function() {
    console.log('Encoded Base 64 File String:', reader.result);
    
    /******************* for Binary ***********************/
    var data=(reader.result).split(',')[1];
     var binaryBlob = atob(data);
     console.log('Encoded Binary File String:', binaryBlob);
  }
  reader.readAsDataURL(file);
}
<input type="file" onchange="uploadFile(this)" />

Post a Comment for "Converting An Image To Binary In Javascript Using Base64"