Created
August 31, 2012 11:30
-
-
Save lyuehh/3551696 to your computer and use it in GitHub Desktop.
nodejs download img
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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