如何在React中对64位编码输入?

jos*_*eph 1 post base64 reactjs axios

我试图用axios发布两个输入,我想在发送之前对它们进行base64编码.

Mos*_*tav 13

考虑使用与and兼容的base-64,它在 React 和 React Native 中为我工作:btoaatob

npm install base-64 --save
Run Code Online (Sandbox Code Playgroud)
npm install base-64 --save
Run Code Online (Sandbox Code Playgroud)

如果您使用的是打字稿,请用于@types/base-64打字

import {decode as base64_decode, encode as base64_encode} from 'base-64';
let encoded = base64_encode('YOUR_DECODED_STRING');
let decoded = base64_decode('YOUR_ENCODED_STRING');
Run Code Online (Sandbox Code Playgroud)


小智 10

const encodedString = new Buffer('your string here').toString('base64');


小智 5

最好使用 Buffer.from('Your String').toString('base64'),因为不推荐使用 new Buffer