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
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)
How to convert image file object to buffer?
Newest Questions - Stack Overflow
javascript - Buffer Through File - Stack Overflow
javascript - Convert Buffer (image) to file - Stack Overflow
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
I hope this will be helpfull for you
const file = new File([
new Blob(["decoded_base64_String"])
], "output_file_name");
You can use one of the various write or writeFile methods which accept a Buffer.
const fs = require("fs");
let buffer = Buffer.from(
"iVBORw0KGgoAAAANSUhEUgAAAAgAAAAGCAIAAABxZ0isAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsMAAA7DAcdvqGQAAAAQSURBVBhXY/iPAwygxP//AAjcj3EdtT3BAAAAAElFTkSuQmCC",
"base64"
);
fs.writeFile("pic.png", buffer, (err) => {
if (err) throw err;
console.log("The file has been saved!");
});