I was wonder how to convert a base64 string to a base 64 url that you could use in the browser for my project. I've already tried using base64.toString('base64url')
, but that didn't work. Below is some example code.
const fs=require("fs")
let base64image=fs.readFileSync("./image.png").toString("base64")
let base64txt=fs.readFileSync("./text.txt").toString("base64")
// converts base64 to a base 64 URL.
function toBase64URL(filetype, base64){ /* ... */ }
console.log(toBase64URL('png', base64image)) // => data:image/png;base64,iVBORw0KGgoAAAANSUhE...
console.log(toBase64URL('txt', base64txt)) // => data:text/plain;base64,dmVyeSBkZXRhaWxlZCA6KQ==