Skip to content

Instantly share code, notes, and snippets.

@siteslave
Forked from sid24rane/base64-encode-decode.js
Created December 19, 2018 16:26
Show Gist options
  • Save siteslave/80af65b70efeed8a4c983346f2d4de28 to your computer and use it in GitHub Desktop.
Save siteslave/80af65b70efeed8a4c983346f2d4de28 to your computer and use it in GitHub Desktop.
Node.js Base64 Encode Decode -> Image
var buffer = require('buffer');
var path = require('path');
var fs = require('fs');
function encode_base64(filename){
fs.readFile(path.join(__dirname,'/public/',filename),function(error,data){
if(error){
throw error;
}else{
var buf = Buffer.from(data);
var base64 = buf.toString('base64');
//console.log('Base64 of ddr.jpg :' + base64);
return base64;
}
});
}
function decode_base64(base64str , filename){
var buf = Buffer.from(base64str,'base64');
fs.writeFile(path.join(__dirname,'/public/',filename), buf, function(error){
if(error){
throw error;
}else{
console.log('File created from base64 string!');
return true;
}
});
}
encode_base64('ddr.jpg');
decode_base64('any_base64_string_goes_here','rane.jpg');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment