This approach worked for me:

function readFileDataAsBase64(e) {
    const file = e.target.files[0];

    return new Promise((resolve, reject) => {
        const reader = new FileReader();

        reader.onload = (event) => {
            resolve(event.target.result);
        };

        reader.onerror = (err) => {
            reject(err);
        };

        reader.readAsDataURL(file);
    });
}

You can call reader.readAsBinaryString() if you wish to use binary string. More here: https://developer.mozilla.org/en-US/docs/Web/API/FileReader

Answer from tomericco on Stack Overflow
🌐
Stack Overflow
stackoverflow.com › questions › 71334124 › how-to-convert-this-file-path-in-buffer-format
reactjs - How to convert this file path in buffer format - Stack Overflow
March 3, 2022 - const chooseFile = async () => { try { const res = await DocumentPicker.pick({ type: [DocumentPicker.types.images] }); console.log("res : " + JSON.stringify(res)); } catch (err) { setFile(null); if (DocumentPicker.isCancel(err)) { } else { throw err; } } }; console.log("file", file); const buffer = new Buffer.from(file); console.log("Buffer", buffer); ... Find the answer to your question by asking. Ask question ... See similar questions with these tags. ... I’m Jody, the Chief Product and Technology Officer at Stack Overflow. Let’s... ... Does a string for fastening a dress need to go through holes to be called a lace or not need to go through holes?
Discussions

React create and download file from buffer array
I have an email attachment file in buffer format I want to display and download the file in ReactJS. More on stackoverflow.com
🌐 stackoverflow.com
ReactJS - FileReader readAsArrayBuffer an image file - Stack Overflow
In a previous post I'd gotten help getting it working in node.js as a proof of concept in transforming local images into binary buffer arrays, but realized too late that I would not be able to use ... More on stackoverflow.com
🌐 stackoverflow.com
How to download buffer data file or blob file in react native?
I am trying to download file using axios in react native. I got a response as arrayBuffer that has init8Array, init16Array and unit8Array, then i convert whole response into unit8Array, but i am tr... More on stackoverflow.com
🌐 stackoverflow.com
node.js - how upload image to mongodb buffer with react in client side - Stack Overflow
I think this a server-side code I have already my code above I used multer and sharp to resize the image and I test it in postman and it works perfectly just want to implement a client-side with react for it I don't know how to convert the image to buffer type in client side and save it in mongodb? More on stackoverflow.com
🌐 stackoverflow.com
October 3, 2019
🌐
npm
npmjs.com › package › file-to-array-buffer
file-to-array-buffer - npm
April 23, 2018 - Turn a `File` (or any `Blob`) into an `ArrayBuffer`.. Latest version: 1.0.0, last published: 8 years ago. Start using file-to-array-buffer in your project by running `npm i file-to-array-buffer`. There are no other projects in the npm registry ...
      » npm install file-to-array-buffer
    
🌐
LinkedIn
linkedin.com › pulse › document-buffer-stream-convert-blob-download-sreeju-r-ogt2c
Document Buffer Stream Convert to Blob and Download
February 26, 2024 - I suggest setting up with Vite and using PNPM for package management ... After a successful installation, create a folder named 'Hooks'. Then, create a file named 'useDocument.js' and begin with the function 'useDocument'.
Find elsewhere
🌐
The Web Dev
thewebdev.info › home › how to convert a file input value to a byte array with javascript?
How to Convert a File Input Value to a Byte Array with JavaScript? - The Web Dev
August 1, 2021 - Next, we call reader.readAsArrayBuffer in the file input’s change event listener to read the file into an array buffer.
      » npm install @craftzdog/react-native-buffer
    
Published: Oct 03, 2025
Version: 6.1.1
Author: Feross Aboukhadijeh
🌐
Stack Overflow
stackoverflow.com › questions › 58946947 › how-to-download-buffer-data-file-or-blob-file-in-react-native
How to download buffer data file or blob file in react native?
export const getDocumentDetail = (reference) => { return async dispatch => { getToken().then(token =>{ AxiosBase.get('/document/'+reference,{ responseType : 'arraybuffer', headers : { Authorization : 'Bearer '+token, Accept : 'application/pdf' } }) .then(response => { console.log('Data',response.data) var bufferData = Buffer.from(response.data) var string = new TextDecoder("utf-8").decode(bufferData); console.log('Buffer : ',bufferData,string) dispatch({ type : DOCUMENT_DETAIL, documentDetail : response.data }) }) .catch(error => { console.log('Documents Error : ',error) }) }) } } ... Post your code. ... Your file is a pdf, that's binary data, converting it to a text file won't do any good. What do you want to do with the File? ... How to download the Blob received from the nodejs backend in reactjs to any Operating System in ReactJS?
🌐
Node.js
nodejs.org › api › buffer.html
Buffer | Node.js v26.11.1 Documentation
Each character is encoded using a single byte. Characters that do not fit into that range are truncated and will be mapped to characters in that range. Converting a Buffer into a string using one of the above is referred to as decoding, and converting a string into a Buffer is referred to as encoding.
🌐
Stack Overflow
stackoverflow.com › questions › 58223153 › how-upload-image-to-mongodb-buffer-with-react-in-client-side
node.js - how upload image to mongodb buffer with react in client side - Stack Overflow
October 3, 2019 - router.post('/users/me/avatar', auth, upload.single('avatar'), async (req, res) => { console.log(req.file) const buffer = await sharp(req.file.buffer).resize({ width: 250, height: 250 }).png().toBuffer() req.user.avatar = buffer await req.user.save() res.send() }, (error, req, res, next) => { res.status(400).send({ error: error.message }) }) node.js · reactjs ·
🌐
Reddit
reddit.com › r/node › how to convert image file object to buffer?
r/node on Reddit: How to convert image file object to buffer?
September 5, 2021 -

I recieve image file object in formdata from user. On server I use formidable to parse that data. Then I have to convert that file to buffer to store in database. I am working in a nextjs project.

What I have tried so far --

Client side code:

let imgFile = document.querySelector("#imageInp").files[0];
let formData = new FormData();
formData.append("image", imgFile);
fetch("http://localhost:3000/data", { 
     method: 'POST',
     body: formData,
});

Server side code:

const data = await new Promise((resolve, reject) => { 
     const form = formidable(); 
     form.parse(req, (err, fields, files) => {
         if (err) reject({ err });
         resolve({ err: null, image: files.image, });
     });
});
if(data.err) return;

const arrBuf = await new Response(data.image).arrayBuffer();
const buffer = Buffer.from(arrBuf);

console.log("image size", data.image.size);
console.log("Array Buffer size", arrBuf.byteLength);
console.log("Buffer size", buffer.byteLength);

Output on console in server:

image size 444053
Array Buffer size 15
Buffer size 15

The code runs but it is not working as expected. The size of imgFile is arround 100 thousand bytes but the arrBuf.byteLength is coming out to be 15.

I tried to reproduce this again in another simple nodejs project but it is giving error that Response is not defined.

I have seen other answers on the internet but they tend to assume that the file is a text file and then they create buffer from its text.

There is blob.arrayBuffer() method but that feature is experimental in nodejs.

Edit: I am using Input(type file) element to receive images. I am sending images to server using fetch post request by attaching FormData to body of the request. Image files are of type image/jpeg. Response obj

🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › API › FileReader › readAsArrayBuffer
FileReader: readAsArrayBuffer() method - Web APIs | MDN
July 26, 2024 - The readAsArrayBuffer() method of the FileReader interface is used to start reading the contents of a specified Blob or File. When the read operation is finished, the readyState property becomes DONE, and the loadend event is triggered. At that time, the result property contains an ArrayBuffer ...
Starred by 133 users
Forked by 8 users
Languages: JavaScript
🌐
CodePal
codepal.ai › code-generator › query › TCOadNdj › convert-buffer-to-file-in-react-and-link-to-show-file
Convert Buffer to File in React - JavaScript
* * @example * const buffer = ...sbG8sIFdvcmxkIQ==" download="example.txt">Download and Show File</a> */ function convertBufferToFile(buffer, fileName) { // Create a Blob object from the buffer const blob = new Blob([buffer]); ...
🌐
JavaScript in Plain English
javascript.plainenglish.io › downloading-and-saving-binary-files-using-react-native-with-expo-451b46cb0cc7
Downloading and Saving binary files using React Native with Expo | by Colin Wren | JavaScript in Plain English
July 6, 2022 - Turn the response’s data in to a base64 string using Buffer · Get a path for the file to be saved under · Write the file to the path in order to have it available to share · Initiate the device’s sharing functionality to share the file · The user then has the option to save the file to the file system or share it elsewhere ·