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
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
const file = e.target.files[0];
// we need to get the raw bytes
const buffer = await file.arrayBuffer();
// each entry of array should contain 8 bits
const bytes = new Uint8Array(buffer);
React create and download file from buffer array
ReactJS - FileReader readAsArrayBuffer an image file - Stack Overflow
How to download buffer data file or blob file in react native?
node.js - how upload image to mongodb buffer with react in client side - Stack Overflow
That looks like you've got a string which represents the body of your file.
You just have to make a new buffer with it.
var fileBuffer = Buffer.from(file)
If your encoding is NOT utf8 you can specify an alternate encoding as a second optional argument.
var fileBuffer = Buffer.from(file, 'base64')
If the file is actually on disk, this is even easier, since, by default, the fs.readFile operation returns a buffer.
fs.readFile(file, function(err, buffer){})
If you're in a real old version of node Buffer.from doesn't exist and you have to use a very memory-unsafe new constructor. Please consider upgrading your node instance to support Buffer.from
In 2024 this is what I'm doing – which makes typescript happy.
First create an ArrayBuffer from the file, then pass the ArrayBuffer to Buffer.from like so
const arrayBuffer = await file.arrayBuffer()
const buffer = Buffer.from(arrayBuffer)
» npm install file-to-array-buffer
» npm install @craftzdog/react-native-buffer
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