到目前为止,我已经有了这个:
const Fs = require('fs')
const Path = require('path')
const Axios = require('axios')
var {Base64Encode} = require('base64-stream');
const url = 'https://unsplash.com/photos/AaEQmoufHLk/download?force=true'
const response = await Axios({
url,
method: 'GET',
responseType: 'stream'
})
response.data.pipe(new Base64Encode())
这个base64编码了图片,我们如何将其转换为字符串?我尝试过类似于以下的方法:
function streamToString (stream) {
const chunks = []
return new Promise((resolve, reject) => {
stream.on('data', chunk => chunks.push(chunk))
stream.on('error', reject)
stream.on('end', () => resolve(Buffer.concat(chunks).toString('utf8')))
})
}
const result = await streamToString(response.data.pipe(new Base64Encode()))
但它出现了错误。有何想法?
Buffer.concat(chunks).toString('utf8')
替换为chunks.join("")
。 - blex