【发布时间】:2019-04-01 12:43:05
【问题描述】:
我尝试使用got 下载图像,并使用responsetype 的Buffer 接口将其转换为base64 编码的字符串。我当前的 sn-p 转换图像并将编码的字符串记录到控制台:
'use strict';
const got = require('got');
const imgUrl = 'https://www.google.de/images/branding/googlelogo/2x/googlelogo_color_272x92dp.png'
got(imgUrl, {
responseType: 'buffer'
})
.then(response => Buffer.from(response.body, 'binary').toString('base64'))
.then(console.log)
我通过将任何终端输出重定向到这样的文件,将 base64 编码的字符串写入文件:
node base64.js >> base64_image
我打开文件并将其内容复制到online base64-image-viewer,其中显示了损坏的图像符号而不是所需的图像。
我的下载和编码方法有问题还是我错过了其他东西?如何缩小问题范围以修复此错误?
【问题讨论】:
标签: node.js image download base64 encode