Categories
JavaScript Answers

How to convert Base64 String to JavaScript file object like as from file input form?

Spread the love

To convert Base64 String to JavaScript file object like as from file input form, we use fetch.

For instance, we write

const url = "data:image/png;base6....";
const res = await fetch(url);
const blob = await res.blob();
const file = new File([blob], "File name", { type: "image/png" });

to call fetch with the base64 URL.

Then we call res.blob to return a promise with the blob response.

Finally, we create a file with the blob with the File constructor.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *