1. <tfoot id='3ttWp'></tfoot>
    <legend id='3ttWp'><style id='3ttWp'><dir id='3ttWp'><q id='3ttWp'></q></dir></style></legend>

      <i id='3ttWp'><tr id='3ttWp'><dt id='3ttWp'><q id='3ttWp'><span id='3ttWp'><b id='3ttWp'><form id='3ttWp'><ins id='3ttWp'></ins><ul id='3ttWp'></ul><sub id='3ttWp'></sub></form><legend id='3ttWp'></legend><bdo id='3ttWp'><pre id='3ttWp'><center id='3ttWp'></center></pre></bdo></b><th id='3ttWp'></th></span></q></dt></tr></i><div id='3ttWp'><tfoot id='3ttWp'></tfoot><dl id='3ttWp'><fieldset id='3ttWp'></fieldset></dl></div>
      • <bdo id='3ttWp'></bdo><ul id='3ttWp'></ul>

      <small id='3ttWp'></small><noframes id='3ttWp'>

    1. Node.js base64 对下载的图像进行编码以在数据 URI 中使用

      Node.js base64 encode a downloaded image for use in data URI(Node.js base64 对下载的图像进行编码以在数据 URI 中使用)

      <tfoot id='L8IxK'></tfoot>
        • <small id='L8IxK'></small><noframes id='L8IxK'>

            <i id='L8IxK'><tr id='L8IxK'><dt id='L8IxK'><q id='L8IxK'><span id='L8IxK'><b id='L8IxK'><form id='L8IxK'><ins id='L8IxK'></ins><ul id='L8IxK'></ul><sub id='L8IxK'></sub></form><legend id='L8IxK'></legend><bdo id='L8IxK'><pre id='L8IxK'><center id='L8IxK'></center></pre></bdo></b><th id='L8IxK'></th></span></q></dt></tr></i><div id='L8IxK'><tfoot id='L8IxK'></tfoot><dl id='L8IxK'><fieldset id='L8IxK'></fieldset></dl></div>
              <tbody id='L8IxK'></tbody>
                <bdo id='L8IxK'></bdo><ul id='L8IxK'></ul>
                <legend id='L8IxK'><style id='L8IxK'><dir id='L8IxK'><q id='L8IxK'></q></dir></style></legend>

                本文介绍了Node.js base64 对下载的图像进行编码以在数据 URI 中使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

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

                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

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

                iVBORw0KGgoAAAANSUhEUgAAARMAAABfCAMAAAD8mtMpAAADAFBMVEUAcwsOcRojZitLflOWBR+aBiGQFiipCSS8DCm1Cya1FiyNKzexKTjDDSrLDS

                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?

                解决方案

                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.

                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.

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

                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.

                EDIT

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

                这篇关于Node.js base64 对下载的图像进行编码以在数据 URI 中使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                相关文档推荐

                Fetch multiple links inside foreach loop(在 foreach 循环中获取多个链接)
                Backbone Fetch Request is OPTIONS method(Backbone Fetch Request 是 OPTIONS 方法)
                Fetch API leaks memory in Chrome(Fetch API 在 Chrome 中泄漏内存)
                How can I download and save a file using the Fetch API? (Node.js)(如何使用 Fetch API 下载和保存文件?(Node.js))
                Send blob data to node using fetch, multer, express(使用 fetch、multer、express 将 blob 数据发送到节点)
                Sending a custom User-Agent string along with my headers (fetch)(发送自定义用户代理字符串以及我的标头(获取))

                <small id='Id9MW'></small><noframes id='Id9MW'>

                  <bdo id='Id9MW'></bdo><ul id='Id9MW'></ul>

                  1. <legend id='Id9MW'><style id='Id9MW'><dir id='Id9MW'><q id='Id9MW'></q></dir></style></legend>

                        <tbody id='Id9MW'></tbody>

                      <i id='Id9MW'><tr id='Id9MW'><dt id='Id9MW'><q id='Id9MW'><span id='Id9MW'><b id='Id9MW'><form id='Id9MW'><ins id='Id9MW'></ins><ul id='Id9MW'></ul><sub id='Id9MW'></sub></form><legend id='Id9MW'></legend><bdo id='Id9MW'><pre id='Id9MW'><center id='Id9MW'></center></pre></bdo></b><th id='Id9MW'></th></span></q></dt></tr></i><div id='Id9MW'><tfoot id='Id9MW'></tfoot><dl id='Id9MW'><fieldset id='Id9MW'></fieldset></dl></div>
                    • <tfoot id='Id9MW'></tfoot>