Skip to content

Instantly share code, notes, and snippets.

@lyuehh
Created August 31, 2012 11:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lyuehh/3551696 to your computer and use it in GitHub Desktop.
Save lyuehh/3551696 to your computer and use it in GitHub Desktop.
nodejs download img
var http = require('http');
var fs = require('fs');
http.get('http://25.media.tumblr.com/tumblr_m98u7dXTPQ1qz7hmlo1_500.jpg',function (res) {
console.log(res.statusCode);
console.log(JSON.stringify(res.headers));
var imgdata = "";
res.setEncoding('binary');
res.on('data',function (chunk) {
imgdata += chunk;
});
res.on('end',function () {
fs.writeFile('a.jpg',imgdata,'binary',function (err) {
if(err) throw err;
console.log('saved');
});
});
}).on('error',function (e) {
console.log('Error: ' + e.message);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment