-3

Is there a way to do the following in vanilla JS:

  1. Download file from URL
  2. Base64 encode it
  3. Write the data to the body

Oh and also this will have to be run client side

  • Does this answer your question? [Temporarily unzip a file to view contents within a browser](https://stackoverflow.com/questions/37075591/temporarily-unzip-a-file-to-view-contents-within-a-browser) – edo9k Sep 04 '20 at 19:24
  • These questions have all been answered. But you'll have to search for each of them individually. Break your problem down and try solving a step at a time. On Base64 encoding: https://stackoverflow.com/questions/7370943/retrieving-binary-file-content-using-javascript-base64-encode-it-and-reverse-de On Unzipping/dealing with files in the browser https://stackoverflow.com/questions/37075591/temporarily-unzip-a-file-to-view-contents-within-a-browser – edo9k Sep 04 '20 at 19:26
  • Yeah but how can I download files in pure JS, all the solutions I have found don't work in my specific use case – Macintosh512k Sep 04 '20 at 19:35
  • 2
    The zip tag appears to be irrelevant. – easrng Sep 04 '20 at 20:01
  • use object urls instead of base64 – Endless Sep 06 '20 at 22:53

1 Answers1

-1

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.

easrng
  • 395
  • 4
  • 10