素材牛VIP会员
nodejs 如何读取远程的图片并显示出来?
 这***人  分类:Node.js  人气:1724  回帖:2  发布于6年前 收藏

现在需要这样一个功能: 通过 GET 参数 "url" , 读取图片并显示图片

我现在的代码是:

var http = require('http');
var url  = require('url');
http.createServer(function (req, res) {
    var params = url.parse( req.url , true );
    var IMGS = new imageServer( http , url);
    IMGS.http( params.query.url  , function( data ){
    	res.writeHead(200, {"Content-Type": data.type}); 
    	var img = new Buffer(data.base64, 'base64').toString('binary');
        console.log(data.base64);
    	res.end( img );
    });

}).listen(8124);

var imageServer = function( http , url ){
	var _url  = url;
	var _http = http;

	this.http = function(url , callback , method){
		method      = method || 'GET';
		callback    = callback || function(){};
		var urlData = _url.parse(url);
		var request = _http.createClient(80 , urlData.host).
		                    request(method, urlData.pathname, {"host": urlData.host});

		request.end();

		request.on('response', function (response)
		{
			var type = response.headers["content-type"],
			    body = "";

		    response.setEncoding('binary');
		    response.on('end', function () {
		        var base64 = new Buffer(body, 'binary').toString('base64');
		        var data = {
		        	type   : type ,
		        	base64 : base64 
		        };
		        callback(data);
		        
		    });
		    response.on('data', function (chunk) {
		        if (response.statusCode == 200) body += chunk;
		    });
		});

	};
};

在终端可以看到base64编码后的数据 , 但浏览器只显示空白的图片. 求解

 标签:node.js

讨论这个帖子(2)垃圾回帖将一律封号处理……

Lv6 码匠
素***2 学生 6年前#1

找到问题

//不能为 res.end(); 输出二进制数据

更改后的代码为:

var http = require('http');
var url = require('url');
http.createServer(function(req, res) {

    var params = url.parse(req.url, true);

    var IMGS = new imageServer(http, url);

    IMGS.http(params.query.url, function(data) {
        res.writeHead(200, {"Content-Type": data.type});
        res.write(data.body, "binary");
        res.end();
    });

}).listen(8124);

var imageServer = function(http, url) {
    var _url = url;
    var _http = http;

    this.http = function(url, callback, method) {
        method = method || 'GET';
        callback = callback ||
        function() {};
        var urlData = _url.parse(url);
        var request = _http.createClient(80, urlData.host).
        request(method, urlData.pathname, {
            "host": urlData.host
        });

        request.end();

        request.on('response', function(response) {
            var type = response.headers["content-type"],
                body = "";
            response.setEncoding('binary');
            response.on('end', function() {
                var data = {
                    type: type,
                    body: body
                };
                callback(data);

            });
            response.on('data', function(chunk) {
                if (response.statusCode == 200) body += chunk;
            });
        });

    };
};
Lv6 码匠
zh***ni 职业无 6年前#2
npm install request
var request = require('request');
var fs = require('fs');
request('http://abc.com/abc.png').pipe(fs.createWriteStream('abc.png'));

abc.png 这样就被下载到你本地了。

 文明上网,理性发言!   😉 阿里云幸运券,戳我领取