https://runkit.com/hollerith/5a726441d9a9770013dcce93
拥有copypasta的众多资源提供了获取此特定图像并将其转换为base64编码数据uri的方法。
var requestBase64 = require('base64-img').requestBase64;
var request = require('request');
var got = require("got");
url = 'http://c1.staticflickr.com/2/1442/26088546654_d034205325_n.jpg';
response = await got(url);
const buf = Buffer.from(response.body, 'binary');
const data = buf.toString('base64');
console.log(`data:image/jpeg;base64,${data}`);
requestBase64(url, function (error, response, body) {
console.log(body);
});
request(url, function (error, response, body) {
const buf = Buffer.from(body, 'binary');
const data = buf.toString('base64');
console.log(`data:image/jpeg;base64,${data}`);
});
答案 0 :(得分:2)
您可以将{encoding: null}
传递给request
,它会返回与requestBase64相同的数据。
request(url, {encoding: null}, function (error, response, body) {
const buf2 = Buffer.from(body, 'binary');
const data2 = buf2.toString('base64');
console.log(`data:image/jpeg;base64,${data2}`);
});
类似于got
库,您可以传递编码。
response = await got(url, { encoding: null });