Sometimes, we want to convert base64 string to JavaScript file object like as from file input form.
In this article, we’ll look at how to convert base64 string to JavaScript file object like as from file input form.
How to convert base64 string to JavaScript file object like as from file input form?
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 url
to return a promise with the response.
Then we call blob
to return a promise with the blob.
Then we put thw blob
into the File
constructor to convert it to a file.
Conclusion
To convert base64 string to JavaScript file object like as from file input form, we use fetch
.