5

I am using express and nodejs and am having problems saving facebook profile pictures to my server.

Location of picture: http://profile.ak.fbcdn.net/hprofile-ak-ash2/275619_223605264_963427746_n.jpg

Script Being Used:

    var http = require('http')
    var fs = require('fs')

    var options = {
      host: 'http://profile.ak.fbcdn.net',
      port: 80,
      path: '/hprofile-ak-ash2/275619_223605264_963427746_n.jpg'
    }

    var request = http.get(options, function(res){
      res.setEncoding('binary')

      var imagedata = ''
      res.on('data', function (chunk) {imagedata += chunk})

      res.on('end', function(){
        fs.writeFile('logo.jpg', imagedata, 'binary', function (err) {
          if(err){throw err}
          console.log('It\'s saved!');
        })
      })
    })

The image saves but is empty. Console logging the image data is blank too. I followed this example origionally which does work for me. Just changing the location of the image to the facebook pic breaks the script.

Community
  • 1
  • 1
wilsonpage
  • 17,341
  • 23
  • 103
  • 147

2 Answers2

8

I ended up coming up with a function that worked:

var http    = require('http');
var fs  = require('fs');
var url     = require('url');

var getImg = function(o, cb){
    var port    = o.port || 80,
        url     = url.parse(o.url);

    var options = {
      host: url.hostname,
      port: port,
      path: url.pathname
    };

    http.get(options, function(res) {
        console.log("Got response: " + res.statusCode);
        res.setEncoding('binary')
        var imagedata = ''
        res.on('data', function(chunk){
            imagedata+= chunk; 
        });
        res.on('end', function(){
            fs.writeFile(o.dest, imagedata, 'binary', cb);
        });
    }).on('error', function(e) {
        console.log("Got error: " + e.message);
    });
}

USAGE:

    getImg({
        url: "http://UrlToImage.com",
        dest: __dirname + '/your/path/to/save/imageName.jpg'
    },function(err){
        console.log('image saved!')
    })
wilsonpage
  • 17,341
  • 23
  • 103
  • 147
  • 1
    Facebook's API requires http://graph.facebook.com/:uid/picture which redirects to another URL. Your code tries to download the picture from the first URL which results in a 302 and no image retreved! Any chance to update this piece of code? :-) – Sven Aug 06 '12 at 09:27
5

I know my answer is a little late, but I hope it'll help others we get to this question, so here it is:

Saving the file to the root directory of your Node server can be done this way:

var request = require("request");
var fs = require("fs");
var fbUserId = 4;
var imageLink = "https://graph.facebook.com/"+ fbUserId +"/picture?width=500&height=500";

request(imageLink).pipe(fs.createWriteStream("resultIMG.png"))
    .on('close', function(){
        console.log("saving process is done!");
    });

Of course, you can add any path you want for the image prior the the file name string.

If you still are seeing empty images, set the encoding of the request module to null , like this:

var request = require("request").defaults({ encoding: null });

That should do it.

Or A.
  • 1,220
  • 1
  • 15
  • 28
  • Which path image will stored our application, can you give detail process on it. – sankar muniyappa Jan 23 '18 at 06:23
  • If I understand what you're asking, like the answer states, you can add the path you desire the image to be saved at as the parameter to the `createWriteStream` function before the saved image name, e.g: `request(imageLink).pipe(fs.createWriteStream("/path/to/resultIMG.png"))`. – Or A. Jan 24 '18 at 08:42