Record image to local server

Update

The accepted answer was good for last year, but today I would use the package that everyone else uses: https://github.com/mikeal/request




Original

I am trying to capture the Google logo and save it on my server using node.js.

This is what I have right now and does not work:

var options = { host: 'google.com', port: 80, path: '/images/logos/ps_logo2.png' }; var request = http.get(options); request.on('response', function (res) { res.on('data', function (chunk) { fs.writeFile(dir+'image.png', chunk, function (err) { if (err) throw err; console.log('It\ saved!'); }); }); }); 

How can I make this work?

+43
Mar 14 2018-11-11T00:
source share
4 answers

A few things happening here:

  • I assume you need fs / http and set the dir variable :)
  • google.com redirects to www.google.com, so you save the body of the redirect response, not the image
  • the answer is transmitted. this means that the data event is triggered many times, not once. you need to save and combine all the pieces together to get the full response body.
  • since you get binary data, you need to set the encoding in response and writeFile, respectively (by default - utf8)

This should work:

 var http = require('http') , fs = require('fs') , options options = { host: 'www.google.com' , port: 80 , path: '/images/logos/ps_logo2.png' } var request = http.get(options, function(res){ var imagedata = '' res.setEncoding('binary') res.on('data', function(chunk){ imagedata += chunk }) res.on('end', function(){ fs.writeFile('logo.png', imagedata, 'binary', function(err){ if (err) throw err console.log('File saved.') }) }) }) 
+72
Mar 14 '11 at 4:08
source share

This thread is old, but I wanted to do the same with the https://github.com/mikeal/request package.

Here is a working example

 var fs = require('fs'); var request = require('request'); // Or with cookies // var request = require('request').defaults({jar: true}); request.get({url: 'https://someurl/somefile.torrent', encoding: 'binary'}, function (err, response, body) { fs.writeFile("/tmp/test.torrent", body, 'binary', function(err) { if(err) console.log(err); else console.log("The file was saved!"); }); }); 
+32
Dec 10 '13 at 9:30
source share

I suggest you use http-request so that you can even control redirects.

 var http = require('http-request'); var options = {url: 'http://localhost/foo.pdf'}; http.get(options, '/path/to/foo.pdf', function (error, result) { if (error) { console.error(error); } else { console.log('File downloaded at: ' + result.file); } }); 
+26
Jan 07 2018-12-17T00:
source share

How about this?

 var http = require('http'), fs = require('fs'), options; options = { host: 'www.google.com' , port: 80, path: '/images/logos/ps_logo2.png' } var request = http.get(options, function(res){ //var imagedata = '' //res.setEncoding('binary') var chunks = []; res.on('data', function(chunk){ //imagedata += chunk chunks.push(chunk) }) res.on('end', function(){ //fs.writeFile('logo.png', imagedata, 'binary', function(err){ var buffer = Buffer.concat(chunks) fs.writeFile('logo.png', buffer, function(err){ if (err) throw err console.log('File saved.') }) }) 
+5
Nov 05 '14 at 3:00
source share



All Articles