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.