5

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 example of how to do this? Will Base64 help me?

Thanks in advance

2

1 Answer 1

11

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)" />

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.