0

This is the situation: I get an image as input, and then I have to display it, and i got no issues with that. Next I need to convert the URL.createObjectURL (event.target.files [0]) to base64, in order to pass it to the backend. Do you have any idea how to do it? I will attach the code below.

const onImageChange = (event) => {
  if (event.target.files && event.target.files[0]) {
    setImage(URL.createObjectURL(event.target.files[0]));
    console.log(event.target.files[0]);
    //var convertedImageBase64 = Convert the image i get from file system to base64
    setImageName(convertedImageBase64);
  }
}
Gigi Carti
  • 53
  • 8
  • Do any of these answers help: https://stackoverflow.com/questions/18650168/convert-blob-to-base64 Also, if possible, consider passing the data to the backend as a blob, and working with it from there. – Dan P Nov 03 '21 at 16:03
  • The fact is that i need to save the image into a mongo database. I noticed that the blob of the same image changes every time it is generated, so I'm not sure if saving the blob in the db is a right choice. – Gigi Carti Nov 03 '21 at 16:16

0 Answers0