将base64图像字符串转换为可以使用node.js提供给浏览器的图像文件 [英] convert a base64 image string to a image file that can be served to browsers using node.js

查看:428
本文介绍了将base64图像字符串转换为可以使用node.js提供给浏览器的图像文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题与Restify不接受HTTP ACCEPT标头有关,图像呈现代码很好。

The issue was to do with the HTTP ACCEPT header not being accepted by Restify, the image rendering code is fine.

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

I have an image encoded as a base64 string, and I want to serve this as an image using node.js. Currently I've got the following code (I'm using Restify) which renders the image in Chrome OK, but the image doesn't render in other browsers (tried IE9, Firefox 4, Android browser):

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();  

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

Anyone able to shed some light on what I might be doing wrong??

谢谢

推荐答案

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

Use data URI syntax, which means you have to prefix the response with the data protocol and the MIME type, plus specify base64 encoding:

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

这篇关于将base64图像字符串转换为可以使用node.js提供给浏览器的图像文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆