繁体   English   中英

将响应身体图像转换为base64

[英]convert RESPONSE BODY image to base64

如何将图像转换为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); }); 

我已使用上面的代码来执行此操作,但是它不起作用。

将请求encoding设置为null并获取文件内容,然后尝试使用节点的核心buffer()功能将其转换为base64

在此处此处查看请求文档

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM