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

And*_*ffe 5 base64 png image cross-browser node.js

问题是Restify不接受HTTP ACCEPT标头,图像呈现代码很好.


我有一个编码为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();  
Run Code Online (Sandbox Code Playgroud)

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

谢谢

Pau*_*tte 2

使用data URI 语法,这意味着您必须在响应中添加数据协议和 MIME 类型前缀,并指定 base64 编码:

res.write("data:image/png;base64,"+decodedBuffer);
Run Code Online (Sandbox Code Playgroud)