Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to base64 encode inputs in React?

I am trying to post two inputs with axios and I want to base64 encode them before I send them.

like image 477
joseph Avatar asked Jun 09 '17 13:06

joseph


People also ask

How do I encode a base64 file?

To base64 encode string you can pipe an echo command into the base64 command-line tool. To ensure no extra, hidden characters are added use the -n flag. Without the -n flag you may capture a hidden characters, like line returns or spaces, which will corrupt your base64 encoding.


Video Answer


3 Answers

Consider using base-64 as well which is compatible with btoa and atob, worked for me in react and react native:

npm install base-64 --save
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');

In case you are using typescript, use @types/base-64 for typing

npm i --save-dev @types/base-64
like image 134
Mostav Avatar answered Oct 01 '22 11:10

Mostav


Deprecated since v6

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

Use Instead

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

like image 43
vinay verma Avatar answered Oct 01 '22 11:10

vinay verma


Is better you use Buffer.from('Your String').toString('base64'), because new Buffer is deprecated

like image 43
Renato Mendes Avatar answered Oct 01 '22 13:10

Renato Mendes