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 Overflow
Discussions

How would I convert an image to base64 in reactJS
Base 64 as in each pixel characteristic (intensity, RGB, etc) is given as a value from 0-63 instead of standard 0-255 or 0-65535? I only know c and python, easiest is bit shifting (maybe incorrect way of doing this). Normalize and find a ratio from what base the image is in to go from N bit to 6 Bit. More on reddit.com
🌐 r/learnprogramming
6
1
March 12, 2021
reactjs - React js image to base64 - Stack Overflow
I want to convert an image to base64 from reactjs to save that image in mongo without uploading the image to the server and then converting it if not converting the image directly More on stackoverflow.com
🌐 stackoverflow.com
React-Native: Convert image url to base64 string
I'm building a react native app that needs to store images at base64 string format for offline viewing capabilities. What library / function would give me the best result to store the image as bas... More on stackoverflow.com
🌐 stackoverflow.com
How to convert files to Base64 in React
I have registration for in React where I need to upload files to the server. Those files needs to be Base64 encoded. More on stackoverflow.com
🌐 stackoverflow.com
🌐
Devcamp
utahedu.devcamp.com › dissecting-react-js › guide › converting-image-files-base64-strings
Converting Image Files into Base64 Strings
What we need to do is take the image and convert it into something that can be stored as text and then we can just embed it along with our paragraph tags and everything else. So it looks like this is done. If you click on Show Code right here, you'll see what this generated.
🌐
Medium
medium.com › @mohammadakilshakirhusain › how-to-convert-image-url-and-local-files-to-base64-in-react-native-with-rnfetchblob-45bd1aeef5ca
How to Convert Image URL and Local Files to Base64 in React Native (With RNFetchBlob) | by Mohammadakilshakirhusain | Medium
February 11, 2026 - If you have a remote image URL and need to convert it to Base64, you can use the following function: const convertUrlToBase64 = async (url) => { try { let imagePath = null; const base64 = await RNFetchBlob.config({ fileCache: true }) .fetch("GET", url) .then((resp) => { imagePath = resp.path(); return resp.readFile("base64"); }) .then((data) => { RNFetchBlob.fs.unlink(imagePath); return "data:image/jpeg;base64," + data; }) .catch((err) => { console.log("convertUrlToBase64 error: ", err); return null; }); return base64; } catch (e) { console.log("convertUrlToBase64 Try/Catch error:", e); return null; } };
🌐
Medium
arcdev.medium.com › how-to-convert-an-image-into-base64-in-react-native-cbadbe72ec78
How to convert an image into base64 in React Native | by arcdev.in | Medium
September 26, 2024 - If you’re using Expo in your React Native project, you can use the Expo FileSystem module to convert images to base64.
🌐
Reddit
reddit.com › r/learnprogramming › how would i convert an image to base64 in reactjs
r/learnprogramming on Reddit: How would I convert an image to base64 in reactJS
March 12, 2021 -

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

Find elsewhere
🌐
npm
npmjs.com › package › react-file-image-to-base64
react-file-image-to-base64 - npm
May 24, 2022 - A simple React Component that helps converts image files from file type to base64 type.
      » npm install react-file-image-to-base64
    
🌐
npm
npmjs.com › package › image-to-base64
image-to-base64 - npm
It's a thing you can use in many situations, for example you can just save the base64 string in your database and increment it in the front-end with the <img> tag in HTML. ... const imageToBase64 = require('image-to-base64'); //or //import ...
      » npm install image-to-base64
    
Published: Apr 09, 2021
Version: 2.2.0
🌐
Medium
medium.com › nerd-for-tech › how-to-store-an-image-to-a-database-with-react-using-base-64-9d53147f6c4f
How to store an image to a database with React using Base 64. | by Omoyemi Arigbanla | Medium
May 15, 2021 - The handleFileUpload function targets the file to be uploaded and the file is asynchronously converted to base64 by the convertToBase64 function. The encoded data is then used to update the postImage state.
🌐
npm
npmjs.com › package › react-native-image-base64
react-native-image-base64 - npm
If you encounter OOM errors on ... on Android might cause very high memory usage. npm install react-native-image-base64 --save or yarn add react-native-image-base64...
      » npm install react-native-image-base64
    
Published: Dec 16, 2019
Version: 0.1.4
🌐
YouTube
youtube.com › watch
Convert Image to base64 and Preview Image in React Js - YouTube
In this video, I have explained how to convert image to blob(Base64) and then preview image in react js.To convert an image to a Base64-encoded blob and prev...
Published: February 19, 2023
🌐
StackBlitz
stackblitz.com › edit › react-base64-image
React Base64 Image - StackBlitz
Starter project for React apps that exports to the create-react-app CLI.
🌐
UNPKG
unpkg.com › browse › react-native-convert-image-to-base64@0.1.0 › README.md
UNPKG
## Getting started `npm install react-native-image-base64 --save` or `yarn add react-native-image-base64` ## Installation `$ react-native link react-native-image-base64` ## Usage ```javascript import ImgToBase64 from 'react-native-image-base64'; ImgToBase64.getBase64String('file://youfileurl', ...
🌐
Stack Overflow
stackoverflow.com › questions › 61483046 › convert-local-img-to-base64-with-react-hooks
reactjs - Convert local img to base64 with react hooks - Stack Overflow
April 28, 2020 - const handleFileRead = async (event) => { const file = event.target.files[0]; const base64 = await convertBase64(file); setImage(base64); }; <input type="file" multiple accept="image/gif, image/jpeg, image/png" onChange={(e) => handleFileRead(e)} />