Skip to content

Instantly share code, notes, and snippets.

@cggaurav
Forked from jfsiii/img2data.js
Created November 5, 2013 12:22
Show Gist options
  • Save cggaurav/7318263 to your computer and use it in GitHub Desktop.
Save cggaurav/7318263 to your computer and use it in GitHub Desktop.
/*
* Based on https://gist.github.com/583836 from http://stackoverflow.com/questions/3709391/node-js-base64-encode-a-downloaded-image-for-use-in-data-uri.
* Neither that gist nor this one work for me in 0.2.x or 0.3.x.
*/
var request = require('request'),
BufferList = require('bufferlist').BufferList,
sys = require('sys'),
bl = new BufferList(),
url = 'http://nodejs.org/logo.png'
;
request({uri:url, responseBodyStream: bl}, function (error, response, body)
{
if (!error && response.statusCode == 200) {
var type = response.headers["content-type"];
var prefix = "data:" + type + ";base64,";
var base64 = new Buffer(bl.toString(), 'binary').toString('base64');
var data = prefix + base64;
console.log(data);
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment