Is there a way to do the following in vanilla JS:
- Download file from URL
- Base64 encode it
- Write the data to the body
Oh and also this will have to be run client side
Is there a way to do the following in vanilla JS:
Oh and also this will have to be run client side
Try this:
let f = new FileReader();
f.onload = () => {
document.body.append(document.createTextNode(f.result.split(",")[1]))
};
fetch("Your URL").then(r=>r.blob()).then(r=>f.readAsDataURL(r))
It downloads the file using the fetch
API, then uses a FileReader
to get a base64 data URL. The data URL is then split, and only the base64 data is kept. A text node containing the base64 text is then appended to the body
.