实现目录和代码如下
目录
要转成base64的文件和index.js放在同一个目录下面,然后
打开cmd,进到相应目录下,运行index.js文件就可以了 如图
index.js 文件如下
var fs = require('fs');
// function to encode file data to base64 encoded string
function base64_encode(file) {
// read binary data
var bitmap = fs.readFileSync(file);
// convert binary data to base64 encoded string
return new Buffer(bitmap).toString('base64');
}
// function to create file from base64 encoded string
function base64_decode(base64str, file) {
// create buffer object from base64 encoded string, it is important to tell the constructor that the string is base64 encoded
var bitmap = new Buffer(base64str, 'base64');
// write buffer to file
fs.writeFileSync(file, bitmap);
console.log('******** File created from base64 encoded string ********');
}
function base64_file(base64str,file){
var bitmap = new Buffer(base64str);
fs.writeFileSync(file, bitmap);
console.log('******** base64 encoded string ********');
}
// input
var base64str = base64_encode('1.png');
// output
base64_file(base64str,'1.png.base64.txt')