您的位置:首页 > 其它

图片 音频等文件转成base64编码

2016-02-29 17:10 295 查看
实现目录和代码如下

目录



要转成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')
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: