nodejs远程获取图片

if(require("http")) {
            var http = require("http");
            http.get(url,function (res) {
                res.setEncoding('binary');//二进制(binary)
                var re ='';
                res.on('data',function (data) {
                    re += data;
                    // collect the data chunks to the variable named "html"

                }).on('end', function () {
                        //console.log(re);
                        var key = getKeyByURL(url);
                        var b = new Buffer(re);
                        var imageData =toArrayBuffer(b)
                         getCanvasData(key, imageData, order);
                    })
            }).on('error', function (e) {
                    console.log("Got error: " + e.message);
                });
        }

    };

function getCanvasData (key, buffer, order) {
createCanvas();
var _blob = new Blob(new Array(buffer), { type: "image/jpeg" });
var _url = URL.createObjectURL(_blob);
var _img = new Image();
var imgData = undefined;
_img.onload = function () {
ctx.clearRect(0, 0, 256, 256);
ctx.drawImage(this, 0, 0);
URL.revokeObjectURL(_url);
imgData = ctx.getImageData(0, 0, 256, 256);
cache[order](key, imgData);
exeCheckCallback(key, imgData);
};
_img.onerror = function () {
exeCheckCallback(key, imgData);
};
_img.src = _url;
};
function createCanvas() {
if (!canvas) {
canvas = document.createElement('canvas');
canvas.width = 256;
canvas.height = 256;
ctx = canvas.getContext('2d');
}
}
上一篇:MyEclipse项目上有个感叹号


下一篇:dede织梦栏目页和文章页中获取当前栏目名称方法