30秒学会 JavaScript 片段 · 2018年1月21日

30秒学会 JavaScript 片段 – btoa

Creates a base-64 encoded ASCII string from a String object in which each character in the string is treated as a byte of binary data.

Create a Buffer for the given string with binary encoding and use Buffer.toString('base64') to return the encoded string.

代码片段

const btoa = str => Buffer.from(str, 'binary').toString('base64');

使用样例

btoa('foobar'); // 'Zm9vYmFy'