当前位置:首页 > JavaScript

js实现base 64

2026-03-15 13:46:05JavaScript

Base64 编码与解码的实现

在 JavaScript 中,可以通过内置的 btoa()atob() 函数实现 Base64 的编码与解码。btoa() 用于将字符串编码为 Base64,atob() 用于将 Base64 字符串解码为原始内容。

编码字符串为 Base64

使用 btoa() 函数对字符串进行 Base64 编码:

const originalString = 'Hello, World!';
const encodedString = btoa(originalString);
console.log(encodedString); // 输出 "SGVsbG8sIFdvcmxkIQ=="

解码 Base64 字符串

使用 atob() 函数将 Base64 字符串解码为原始内容:

js实现base 64

const encodedString = 'SGVsbG8sIFdvcmxkIQ==';
const decodedString = atob(encodedString);
console.log(decodedString); // 输出 "Hello, World!"

处理 Unicode 字符

btoa()atob() 仅支持 ASCII 字符。如果字符串包含 Unicode 字符(如中文、表情符号等),需先进行转义处理:

// 编码 Unicode 字符串
function encodeUnicode(str) {
    return btoa(encodeURIComponent(str).replace(/%([0-9A-F]{2})/g, (match, p1) => {
        return String.fromCharCode('0x' + p1);
    }));
}

// 解码 Unicode 字符串
function decodeUnicode(str) {
    return decodeURIComponent(atob(str).split('').map(c => {
        return '%' + ('00' + c.charCodeAt(0).toString(16)).slice(-2);
    }).join(''));
}

const unicodeString = '你好,世界!';
const encodedUnicode = encodeUnicode(unicodeString);
console.log(encodedUnicode); // 输出编码后的 Base64 字符串

const decodedUnicode = decodeUnicode(encodedUnicode);
console.log(decodedUnicode); // 输出 "你好,世界!"

Node.js 环境中的实现

在 Node.js 中,可以使用 Buffer 实现 Base64 编码与解码:

js实现base 64

// 编码为 Base64
const originalData = 'Hello, Node.js!';
const encodedData = Buffer.from(originalData).toString('base64');
console.log(encodedData); // 输出 "SGVsbG8sIE5vZGUuanMh"

// 解码 Base64
const decodedData = Buffer.from(encodedData, 'base64').toString();
console.log(decodedData); // 输出 "Hello, Node.js!"

二进制数据的 Base64 处理

若需处理二进制数据(如文件),可通过 FileReader(浏览器)或 Buffer(Node.js)实现:

浏览器环境:

// 读取文件并编码为 Base64
const fileInput = document.getElementById('fileInput');
fileInput.addEventListener('change', (e) => {
    const file = e.target.files[0];
    const reader = new FileReader();
    reader.onload = (event) => {
        const base64String = event.target.result.split(',')[1];
        console.log(base64String); // 输出文件的 Base64 编码
    };
    reader.readAsDataURL(file);
});

Node.js 环境:

const fs = require('fs');
const fileData = fs.readFileSync('example.jpg');
const base64Data = fileData.toString('base64');
console.log(base64Data); // 输出文件的 Base64 编码

以上方法覆盖了常见场景下的 Base64 编码与解码需求,包括字符串、Unicode 字符及二进制数据的处理。

标签: jsbase
分享给朋友:

相关文章

js防抖和节流实现

js防抖和节流实现

防抖(Debounce)的实现 防抖的核心思想是在事件被触发后,延迟执行回调函数。如果在延迟时间内再次触发事件,则重新计时。适用于输入框搜索、窗口大小调整等场景。 function debounce…

js类实现

js类实现

使用类实现 JavaScript 功能 在 JavaScript 中,类(Class)是一种语法糖,基于原型继承机制。通过 class 关键字可以更直观地定义对象模板。 基本类定义 class…

js实现上传文件

js实现上传文件

文件上传的基本实现 使用HTML的<input type="file">元素配合JavaScript的File API可以实现文件上传功能。 <input type="file"…

js 实现跳转

js 实现跳转

使用 window.location.href 进行跳转 通过修改 window.location.href 可以跳转到指定 URL,浏览器会加载新页面: window.location.hre…

js实现滑动

js实现滑动

实现滑动效果的方法 在JavaScript中实现滑动效果可以通过多种方式完成,以下是几种常见的实现方法: 使用CSS过渡和JavaScript触发 通过CSS定义过渡效果,JavaScript控制触…

js 实现页面刷新

js 实现页面刷新

使用 location.reload() location.reload() 是原生 JavaScript 提供的页面刷新方法。调用该方法会重新加载当前页面,相当于点击浏览器的刷新按钮。 // 基本…