0

I'm trying to download and serve an image with Node.js, but when I get the image it ends with a plus sign:

[data]...9QAPvv70AXFAh2KLvv70/77+977+9

When I embed the data into an HTML image using data:image/jpg;base64, it displays a question mark (invalid image). I'm using this code:

request(image_url, function (error, response, body) {
  if (!error && response.statusCode == 200) {

    var data_uri_prefix = 'data:' + response.headers['content-type'] + ';base64,';

    var image = new Buffer(body).toString('base64');

    // To ease testing for now
    console.log(image);
  }
});

I've tried including the 'binary' parameter when creating the Buffer, among other things. Additionally, the headers don't display any type of special encoding. I'm not really sure why the image is invalid. Is there something I'm missing in the process?

apparatix
  • 1,492
  • 7
  • 22
  • 37
  • I think this question is duplicate of https://stackoverflow.com/questions/3709391/node-js-base64-encode-a-downloaded-image-for-use-in-data-uri There is some solution also. – rutsky Mar 08 '15 at 03:29
  • 3
    Probably related to [Getting binary content in Node.js using request](http://stackoverflow.com/questions/14855015/getting-binary-content-in-node-js-using-request). By default, `request` will assume the response is of UTF-8 data and will prepare `body` as a String. You have to tell it when you expect a binary response. – Jonathan Lonowski Mar 08 '15 at 03:56

0 Answers0