如下面的代码所示,我可以将base64编码的流写入文件,但我想将base64编码的字符串分配给变量而不是创建文件,以便我可以将它用于我想要的目的。如何将response.pipe(base64.encode());
作为base64编码的字符串分配给变量?我使用base64-stream
来编码传入的流。
const request = xero.call('GET', `/Invoices/${invoiceId}`);
request.setHeader('Accept', 'application/pdf');
request.on('response', async (response) => {
const data = response.pipe(base64.encode());
const file = fs.createWriteStream(`${invoiceId}.txt`);
data.pipe(file);
});
request.end();
答案 0 :(得分:0)
要将流的内容存储为字符串,您需要连接base64流,然后将该缓冲区存储为变量。为此,我喜欢使用concat-stream
。
使用const data = response.pipe(base64.encode());
但是
data.pipe(concat(buf => {
const str = buf.toString('base64')
})