Nodejs base64编码与解码

1、普通字符串

//编码

new Buffer(String).toString('base64');

//解码

new Buffer(base64Str, 'base64').toString();


2、十六进制Hex

//编码

new Buffer(String, 'base64').toString('hex');

//解码

new Buffer(base64Str, 'hex').toString('utf8');



3、图片

const fs = require('fs');

//编码

function base64_encode(file) {

    let bitmap = fs.readFileSync(file);

    return new Buffer(bitmap).toString('base64');

}

//解码

function base64_decode(base64str, file) {

    var bitmap = new Buffer(base64str, 'base64');

    fs.writeFileSync(file, bitmap);

}


声明:本站所有文章和图片,如无特殊说明,均为原创发布,转载请注明出处。
随机推荐
Express 使用 cookie-parser 处理 cookies
WordPress 增强编辑器功能
版权相关
WordPress 分类添加自定义字段
Wordpress 使用 tag 标签获取文章列表的方法
SQL 注入
Git 从远程仓库拉取代码覆盖本地代码
在 HTML 中引入 CSS