Sometimes, we want to convert data URL to file object in JavaScript.
In this article, we’ll look at how to convert data URL to file object in JavaScript.
How to convert data URL to file object in JavaScript?
To convert data URL to file object in JavaScript, we use fetch
.
For instance, we write
const res = await fetch(src);
const buf = await res.arrayBuffer();
const file = new File([buf], fileName, { type: mimeType });
to call fetch
with the src
data URL string.
Then we call res.arrayBuffer
to get an array buffer from the URL.
Next we create a File
object with the buf
buffer, fileName
string, and the mimeType
MIME type string.
Conclusion
To convert data URL to file object in JavaScript, we use fetch
.