Client side js. With FileReader and fetch.
const base64 = await fetch('https://i.imgur.com/gBi0yjr.png')
.then(response => response.blob())
.then(blob => {
const reader = new FileReader();
reader.readAsDataURL(blob);
return new Promise((res) => {
reader.onloadend = () => {
res(reader.result);
}})
})
Answer from Robert on Stack OverflowClient side js. With FileReader and fetch.
const base64 = await fetch('https://i.imgur.com/gBi0yjr.png')
.then(response => response.blob())
.then(blob => {
const reader = new FileReader();
reader.readAsDataURL(blob);
return new Promise((res) => {
reader.onloadend = () => {
res(reader.result);
}})
})
Give it a go:
var request = require('request').defaults({ encoding: null });
request.get(/* URL FIREBASE */, function (error, response, body) {
if (!error && response.statusCode == 200) {
data = Buffer.from(body).toString('base64');
console.log(data);
}
});
I use rn-fetch-blob, basically it provides lot of file system and network functions make transferring data pretty easy.
react-native-fetch-blob is deprecated
import RNFetchBlob from "rn-fetch-blob";
const fs = RNFetchBlob.fs;
let imagePath = null;
RNFetchBlob.config({
fileCache: true
})
.fetch("GET", "http://www.example.com/image.png")
// the image is now dowloaded to device's storage
.then(resp => {
// the image path you can use it directly with Image component
imagePath = resp.path();
return resp.readFile("base64");
})
.then(base64Data => {
// here's base64 encoded image
console.log(base64Data);
// remove the file from storage
return fs.unlink(imagePath);
});
source Project Wiki
There is a better way: Install this react-native-fs, IF you don't already have it.
import RNFS from 'react-native-fs';
RNFS.readFile(this.state.imagePath, 'base64')
.then(res =>{
console.log(res);
});
Deprecated since v6
const encodedString = new Buffer('your string here').toString('base64');
Use Instead
const encodedString = Buffer.from('your string here').toString('base64');
If you get this error Uncaught ReferenceError: Buffer is not defined
you may add dependency on buffer into your project
npm install --save buffer
update your webpack.config.js
const webpack = require("webpack");
module.exports = {
plugins: [
// add this line to plugins
new webpack.ProvidePlugin({
Buffer: ['buffer', 'Buffer'],
}),
],
resolve: {
fallback: {
// add this line to resolve.fallback
"buffer": require.resolve("buffer")
}
},
}
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
file reading is asynchronous. so use callback or promise to solve your problem.
let idCardBase64 = '';
this.getBase64(idCard, (result) => {
idCardBase64 = result;
});
and use callback to return the data which you get.
getBase64(file, cb) {
let reader = new FileReader();
reader.readAsDataURL(file);
reader.onload = function () {
cb(reader.result)
};
reader.onerror = function (error) {
console.log('Error: ', error);
};
}
Also, you can use this React component React File Base64
Check the demo