Node.js的连接的base64 code下载的图像数据使用URI [英] Node.js base64 encode a downloaded image for use in data URI

查看:400
本文介绍了Node.js的连接的base64 code下载的图像数据使用URI的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用节点v0.2.0我试图从服务器获取的图像,将其转换成一个base64字符串,然后在图像标签嵌入在网页上。我有以下的code:

Using Node v0.2.0 I am trying to fetch an image from a server, convert it into a base64 string and then embed it on the page in an image tag. I have the following code:

var express = require('express'),
request = require('request'),
sys = require('sys');

var app = express.createServer(
    express.logger(),
    express.bodyDecoder()
);

app.get('/', function(req, res){

    if(req.param("url")) {
        var url = unescape(req.param("url"));
        request({uri:url}, function (error, response, body) {
          if (!error && response.statusCode == 200) {

                var data_uri_prefix = "data:" + response.headers["content-type"] + ";base64,";
                var buf = new Buffer(body);
                var image = buf.toString('base64');

                image = data_uri_prefix + image;

                res.send('<img src="'+image+'"/>');

          }
        });
    }
});

app.listen(3000);

注:此code要求前preSS request 。当然,节点。如果你已经安装了故宫,它应该是那样简单故宫安装前preSS或故宫安装的要求。

Note: This code requires "express" and "request". And of course, node. If you have npm installed, it should be as simple as "npm install express" or "npm install request".

不幸的是,预计今年不起作用。如果我做的谷歌徽标转换,然后我得到以下字符串的开头:

Unfortunately, this doesn't work as expected. If I do the conversion with the Google logo, then I get the following at the beginning of the string:

77+9UE5HDQoaCgAAAA1JSERSAAABEwAAAF8IAwAAAO+/ve+/ve+/vSkAAAMAUExURQBzCw5xGiNmK0t+U++/vQUf77+9BiHvv70WKO+/vQkk77+9D

77+9UE5HDQoaCgAAAA1JSERSAAABEwAAAF8IAwAAAO+/ve+/ve+/vSkAAAMAUExURQBzCw5xGiNmK0t+U++/vQUf77+9BiHvv70WKO+/vQkk77+9D

但是,如果我使用一个在线 Base64编码的连接codeR ,提供相同的图像,那么它完美的作品。该字符串开头是这样的:

However if I use an online Base64 encoder with the same image, then it works perfectly. The string starts like this:

iVBORw0KGgoAAAANSUhEUgAAARMAAABfCAMAAAD8mtMpAAADAFBMVEUAcwsOcRojZitLflOWBR+aBiGQFiipCSS8DCm1Cya1FiyNKzexKTjDDSrLDS

iVBORw0KGgoAAAANSUhEUgAAARMAAABfCAMAAAD8mtMpAAADAFBMVEUAcwsOcRojZitLflOWBR+aBiGQFiipCSS8DCm1Cya1FiyNKzexKTjDDSrLDS

我要去哪里错了,这是不正常工作?我已经尝试了许多不同的js的base64实现,他们都没有以相同的方式工作。我能想到的唯一的事情是我想错了东西转换成的base64,但我应该怎么转换,如果是这样的话?

Where am I going wrong that this isn't working correctly? I have tried so many different js base64 implementations and they all don't work in the same way. The only thing I can think of is that I am trying to convert the wrong thing into base64, but what should I convert if that is the case?

推荐答案

问题是编码和存储在JavaScript字符串二进制数据。有这个缓冲下在 http://nodejs.org/api.html 一个pretty良好的部分。

The problem is encoding and storing binary data in javascript strings. There's a pretty good section on this under Buffers at http://nodejs.org/api.html.

不幸的是,解决这个问题最简单的方式参与改变的要求故宫。我不得不添加 response.setEncoding('二进制'); 上略低于 VAR缓冲行66;在/路径 /to/lib/node/.npm/request/active/package/lib/main.js。这将罚款为此请求而不是其他。因此,这是唯一一套基于其他一些通过选择您可能要破解它。

Unfortunately, the easiest way to fix this involved changing the request npm. I had to add response.setEncoding('binary'); on line 66 just below var buffer; in /path/to/lib/node/.npm/request/active/package/lib/main.js. This will work fine for this request but not others. You might want to hack it so that this is only set based on some other passed option.

我再改 VAR BUF =新的缓冲区(主体) VAR BUF =新的缓冲区(身体,'二进制'); 。在此之后,一切正常。

I then changed var buf = new Buffer(body) to var buf = new Buffer(body, 'binary');. After this, everything worked fine.

另一种方式来做到这一点,如果你真的不想碰触的要求故宫,将是,在responseBodyStream参数要求实现写流对象通过。然后,该对象将在它自己的缓冲器的响应存储流数据。也许有,这是否已经是一个图书馆......我不知道。

Another way to do this, if you really didn't want to touch the request npm, would be to pass in an object that implements Writable Stream in the responseBodyStream argument to request. This object would then store the streamed data from the response in it's own buffer. Maybe there is a library that does this already... i'm not sure.

我要离开这里,现在,但如果你要我澄清什么随意评论。

I'm going to leave it here for now, but feel free to comment if you want me to clarify anything.

修改

查看评论。在 http://gist.github.com/583836 新的解决方案

Check out comments. New solution at http://gist.github.com/583836

这篇关于Node.js的连接的base64 code下载的图像数据使用URI的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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