Skip to content

Instantly share code, notes, and snippets.

@jfsiii
Created January 30, 2011 23:30
Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save jfsiii/803410 to your computer and use it in GitHub Desktop.
Save jfsiii/803410 to your computer and use it in GitHub Desktop.
base64 encoding images in NodeJS
/*
* 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