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 使用 XSS 模块进行安全过滤
WordPress 添加 Favicon 图标的方法
Express.js CSRF 安全防护
WordPress 自定义文章类型
CSRF 双重 Cookie 验证
WordPress 常用函数 / sanitize_user
JavaScript screen对象
github Support for password authentication was removed