【发布时间】:2018-12-28 21:45:36
【问题描述】:
我正在尝试fetch来自服务器的一些二进制数据(MP3),然后将其存储在一个文件中:
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);
}
}
);
})
问题标记在BUG HERE。我将一个 blob 传递给 fs,它不理解 blob,只写 [object Blob]。我还尝试了以下方法:
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
以上都不起作用。如何正确地做到这一点?
请注意,我在调用writeFile 之前记录了blob.size。 blob.size 显示正确的大小,所以 fetch 似乎成功了。
【问题讨论】:
-
您使用的是哪个 fetch 库?
-
节点获取。我会更新问题。