function consume(reader) {
var getReader = reader.body.getReader();
var total = 0;
let fscreateFile = fs.createWriteStream('./src/fetchmusic1.wav')
return new Promise((resolve, reject) => {
function pump() {
getReader.read().then(({done, value}) => {
if (done) {
resolve();
fscreateFile.end()
return ;
}
// 这里的value 是Uint8Array
fscreateFile.write(Buffer.from(value))
total += value.byteLength;
// console.log(`下载字节 ${value.byteLength} bytes (${total} 总下载字节)`);
pump();
}).catch(reject)
}
pump();
});
}
fetch('downfile/9043.wav')
.then(function(data){
consume(data);
return data;
})
.then((data) =>{
console.log(data);
})
.catch((e) => {console.log("something went wrong: " + e)});
使用fetch下载文件
最新推荐文章于 2025-06-27 09:02:38 发布
本文介绍了一种使用JavaScript Fetch API从远程服务器下载音频文件的方法,并通过Node.js将数据写入本地文件的过程。该过程涉及创建一个Promise来处理读取流,并使用fs模块将接收到的数据写入到指定路径。
979

被折叠的 条评论
为什么被折叠?



