2021年3月11日星期四

How would I convert an image to base64 in reactJS

I have this function where I call a function and have a local file as the parameter to convert it to base64.

export const fileToBase64 = (filename, filepath) => {    return new Promise(resolve => {      var file = new File([filename], filepath);      var reader = new FileReader();      // Read file content on file loaded event      reader.onload = function(event) {        resolve(event.target.result);      };        // Convert data to base64      reader.readAsDataURL(file);    });  }  

Importing the function

  fileToBase64("shield.png", "./form").then(result => {        console.log(result);        console.log("here");      });    

gives me an output as

data:application/octet-stream;base64,c2hpZWxkLnBuZw== here

I want to get the base64 string from it. I looked into this post online for guidance https://medium.com/@simmibadhan/converting-file-to-base64-on-javascript-client-side-b2dfdfed75f6

https://stackoverflow.com/questions/66594485/how-would-i-convert-an-image-to-base64-in-reactjs March 12, 2021 at 01:06PM

没有评论:

发表评论