问题
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
回答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)" />
来源:https://stackoverflow.com/questions/51533584/converting-an-image-to-binary-in-javascript-using-base64