gpt4 book ai didi

image - 将 base64 图像字符串转换为可以使用 node.js 提供给浏览器的图像文件

转载 作者:搜寻专家 更新时间:2023-10-31 22:53:12 24 4
gpt4 key购买 nike

问题是 Restify 不接受 HTTP ACCEPT header ,图像渲染代码没问题。


我有一个编码为 base64 字符串的图像,我想使用 node.js 将其作为图像提供。目前我有以下代码(我正在使用 Restify),它可以在 Chrome 中渲染图像,但图像不会在其他浏览器中渲染(试过 IE9、Firefox 4、Android 浏览器):

var decodedBuffer = new Buffer(dataString,"base64");            
res.send({
code: 200,
headers: {'Content-Type': 'image/png', 'Content-Length': decodedBuffer.length},
noEnd: true
});

res.write(decodedBuffer);
res.end();

任何人都能够阐明我可能做错了什么??

谢谢

最佳答案

使用data URI syntax ,这意味着您必须在响应前加上数据协议(protocol)和 MIME 类型,并指定 base64 编码:

res.write("data:image/png;base64,"+decodedBuffer);

关于image - 将 base64 图像字符串转换为可以使用 node.js 提供给浏览器的图像文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6329221/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com