1 回答

TA貢獻1772條經驗 獲得超5個贊
在您的示例中,您使用了Base64encode已經抽出字符串的模塊 - 這使得上面的示例失?。ǔ悄鞘且驗轫敿壍却?。您實際上不需要將塊轉換為字符串,因為它們已經是字符串,因此可以進行簡單的串聯。
事實上,僅使用 node.js 流,整個事情可能會簡單 10 倍:
const Axios = require('axios')
const {PassThrough} = require("stream");
const url = 'https://unsplash.com/photos/AaEQmoufHLk/download?force=true';
(async function() {
const response = await Axios({
url,
method: 'GET',
responseType: 'stream'
});
// we just pipe the data (the input carries it's own encoding)
// to a PassThrough node stream that outputs `base64`.
const chunks = response.data
.pipe(new PassThrough({encoding:'base64'}));
// then we use an async generator to read the chunks
let str = '';
for await (let chunk of chunks) {
str += chunk;
}
// et voila! :)
console.log(str);
})();
添加回答
舉報