[英]convert RESPONSE BODY image to base64
base64
string? 如何将图像转换为base64
字符串? var request = require('request'); let options = { url: imgUrl, headers: { "Authorization": imgauth }, contentType: "base64" }; //request response Content-type : 'image/png' request(options, function(error, response, body) { var base64data = new Buffer(body, 'binary').toString('base64'); console.log(base64data); });
I have used the code above to do so, but it does not work. 我已使用上面的代码来执行此操作,但是它不起作用。
Set request encoding
to null and get the file content and then try to convert it to base64
using node's core buffer()
functionality. 将请求encoding
设置为null并获取文件内容,然后尝试使用节点的核心buffer()
功能将其转换为base64
。
See request documentation here and here 在此处和此处查看请求文档
const Request = require('request').defaults({ encoding: null });
let options = {
url: imgUrl,
headers: { "Authorization": imgauth }
};
Request.get(options, (error, response, body) => {
if (!error && response.statusCode == 200) {
let imagedata = "data:" + response.headers["content-type"] + ";base64," + new Buffer(body).toString('base64');
console.log(imagedata);
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.