I managed to solve this by relatively short lines of code. I take the selectedFile from the state and then convert it to base64. I tested it with the separate button and I got base64 image in the console.
const convertToBase64 = () => {
const reader = new FileReader()
reader.readAsDataURL(selectedFile)
reader.onload = () => {
console.log('called: ', reader)
setBase64IMG(reader.result)
}
}
Answer from Jukka Koivu on Stack OverflowHow would I convert an image to base64 in reactJS
reactjs - React js image to base64 - Stack Overflow
React-Native: Convert image url to base64 string
How to convert files to Base64 in React
I asked this on stack overflow but no one seems to have the solution that I need.
I am having issues with having the ability to convert an image to base64. I have my images stored in the subdirectory of my project. I have this function thats suppose to convert the image to base64, which gives me this
data:application/octet-stream;base64,c2hpZWxkLnBuZw==
at first I thought it was correct, but its suppose to be a a png,
data:image/pgn;base64,c2hpZWxkLnBuZw==
My function
export const fileToBase64 = (filename, filepath) => {
return new Promise(resolve => {
var file = new Image([filename], filepath);
var reader = new FileReader();
// Read file content on file loaded event
reader.onload = function(event) {
resolve(event.target.result);
};
// Convert data to base64
reader.readAsDataURL(file);
});
}
This is where I call that function to try to debug, but having issues
fileToBase64("shield.png", "./pictures/shield.png").then(result => {
console.log(result);
console.log("here");
let buff = new Buffer(result);
let text = buff.toString('ascii');
console.log(text)
});
Code that I used as reference
» npm install react-file-image-to-base64
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);
});
» npm install image-to-base64
» npm install react-native-image-base64
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