I'm trying to fetch
some binary data (MP3) from a server and then store it in a file:
var fs = require ('fs');
var fetch = require ('node-fetch');
fetch (
audioUrl,
{
method: 'GET',
headers: { 'Accept': '*/*' }
}
)
.then ((res) => res.blob())
.then ((blob) => {
console.log ('Blob size: ', blob.size);
fs.writeFile (
`filename.mp3`,
blob, // BUG HERE
(err) => {
if (err) {
console.log (`Error writing audio ${audioIdx}`, err);
}
}
);
})
The problem is marked at BUG HERE
. I'm passing a blob to fs
, which doesn't understand blobs, and simply writes [object Blob]
. I also tried the following:
blob // writes [object Blob]
new Uint8Array (blob) // writes empty file
Buffer.from (new Uint8Array (blob)) // writes empty file
new Buffer (blob, 'binary') // Error: argument is not a Buffer
None of the above work. How to do this correctly?
Note that I'm logging blob.size
before calling writeFile
. blob.size
shows the correct size, so fetch
seems to be successful.
To convert Blob to File in JavaScript, we can use the File constructor. const file = new File([myBlob], "name"); to create a File object with the myBlob blob object in the array. The 2nd argument is the file name string.
So you need to use original blob file reference (if you have it) or convert URL into file instance. To convert image URL into the file itself, you can do the following. async function convertToFile(url){ let response = await fetch(url); let blob = await response. blob(); return new File([blob], 'put_the_name.
In our fetch request example (run fetch request live), we create a new request using the Request() constructor, then use it to fetch a JPG. When the fetch is successful, we read a Blob out of the response using blob() , put it into an object URL using URL.
Ok, so from the MDN Docs a Blob is not a buffer, which is what you need for writing to the file system.
I think you might be better off just getting the buffer from the response, like:
.then(res => res.buffer())
.then(/* write your file here */)
though depending on how big the files are, your best option might be to skip the .writeFile
function altogether and pipe streams instead.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With