Categories
JavaScript Answers

How to convert base64 png data to JavaScript file objects?

Spread the love

Sometimes, we want to convert base64 png data to JavaScript file objects.

In this article, we’ll look at how to convert base64 png data to JavaScript file objects.

How to convert base64 png data to JavaScript file objects?

To convert base64 png data to JavaScript file objects, we use fetch.

For instance, we write

const res = await fetch(url);
const buf = await res.arrayBuffer();
const file = new File([buf], filename, { type: mimeType });

to call fetch with the image url to make a get request to get the image.

Then we get the image as an array buffer with arrayBuffer.

Next we put the array buffer into an array in the File constructor to convert the array buffer to a file with MIME type mimeType.

Conclusion

To convert base64 png data to JavaScript file objects, we use fetch.

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 *