JavaScript提供了两个原生Base64相关方法:
-
btoa():字符串或二进制值转为Base64编码
- atob():Base64编码转为原来的编码
字符串转Base64:
function encode () {
let str = encodeURI("哈哈");
let base64 = btoa(str );
console.log(base64);
}
Base64转字符串:
function decode () {
let haha = atob("哈哈");
let str = decodeURI(haha);
console.log(str);
}