0

I'm trying to load an image (.png/.jpg/*.jpeg) from local disk, to then convert it to base64 to send it via http request as content. what I'm getting is image name/metadata being converted to base64, but not the image itself, when converting the base64 back to an image. my code:

const fs = require("fs");
const imgDir = "./src/ps.png";

function base64_encode(file) {
  // read binary data
  var bitmap = fs.readFileSync(file);
  // convert binary data to base64 encoded string
  return new Buffer(bitmap).toString("base64");
}

var convertedImage = base64_encode(imgDir);

console.log(convertedImage);

also in codesandbox: https://codesandbox.io/s/fast-waterfall-x80q1?file=/src/App.js


edit: to highlight, the output I'm currently getting is aHR0cHM6Ly91cGxvYWRzLmNvZGVzYW5kYm94LmlvL3VwbG9hZHMvdXNlci82MzY2MTA4Ni02MDE3LTRmZTYtYjExOC1hZjM2MDg2NGQ0ODIvcEhLdi1leGVjdXRlZC1wYXltZW50LXN2Z3JlcG8tY29tLnBuZw== which is not a valid/real image

Ibrahim
  • 1
  • 1
  • Does this answer your question? [ReadFile in Base64 Nodejs](https://stackoverflow.com/questions/28834835/readfile-in-base64-nodejs) – User863 Jun 05 '21 at 10:45

0 Answers0