node.js抓取网上图片保存到本地

用到两个模块,http和fs

var http = require("http");
var fs = require("fs");

var server = http.createServer(function(req, res){}).listen(50082);
console.log("http start");

 

var url = "http://s0.hao123img.com/res/img/logo/logonew.png";
http.get(url, function(res){
    var imgData = "";

    res.setEncoding("binary"); //一定要设置response的编码为binary否则会下载下来的图片打不开


    res.on("data", function(chunk){
        imgData+=chunk;
    });

    res.on("end", function(){
        fs.writeFile("./public/upload/downImg/logonew.png", imgData, "binary", function(err){
            if(err){
                console.log("down fail");
            }
            console.log("down success");
        });
    });
});

node.js抓取网上图片保存到本地,布布扣,bubuko.com

node.js抓取网上图片保存到本地

上一篇:Devexpress GridView内嵌dx:ASPxGridLookup取得控件值乱跳解决方案


下一篇:CSS盒子模型元素实际宽度的计算