nodejs 将网络图片转为base64(绕过前端跨域)
2019-03-23 本文已影响0人
webjiacheng
var http = require('http');
var url = 'http://thirdwx.qlogo.cn/mmopen/BEMV4WOAicktAAllnv9FdJFSU7QYVibMU62ctg7Ie5HiaCuDVCyiapibwrs48N97yrzLel03FvOUqAHEZGfBY5tCdnuHKEg4YvZSC/132'; //一张网络图片
http.get(url, function (res) {
var chunks = [];
var size = 0;
res.on('data', function (chunk) {
chunks.push(chunk);
size += chunk.length; //累加缓冲数据的长度
});
res.on('end', function (err) {
var data = Buffer.concat(chunks, size);
var base64Img = data.toString('base64');
console.log(`data:image/png;base64,${base64Img}`);
});
});