使用base64在javascript中将图像转换为二进制

问题描述:

我必须将图像转换为二进制文件,以便通过IPFS进行存储,然后再次将其作为可见图像检索.

I have to convert an image to binary for storing it through IPFS and retrieve it again as a viewable image.

我应该使用javascript代码来完成此操作.是否有任何明确的例子说明如何做到这一点? Base64会帮助我吗?

I should do this with javascript code. Does any body have any clear example of how to do this? Will Base64 help me?

预先感谢

使用文件阅读器:

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