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

Answer from tkone on Stack Overflow
🌐
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 ...
Discussions

How to convert image file object to buffer?
You need to know what the input type is. There is multiple ways of sending files. More on reddit.com
🌐 r/node
17
19
September 5, 2021
Newest Questions - Stack Overflow
Stack Overflow | The World’s Largest Online Community for Developers More on stackoverflow.com
🌐 stackoverflow.com
javascript - Buffer Through File - Stack Overflow
I have a scenario where my producer runs a lot faster than my consumer with a lot of data. However, I do not want to block the producer. So the only option is to buffer the extra data through a fil... More on stackoverflow.com
🌐 stackoverflow.com
javascript - Convert Buffer (image) to file - Stack Overflow
I'm looking for the best way to send image files to my server using Apollo Express, and Node. Getting the information there doesn't seem to be an issue, I convert the object into a string but can't... More on stackoverflow.com
🌐 stackoverflow.com
🌐
Nesin
nesin.io › blog › read-file-buffer-nodejs
How to Read a File as Buffer in Node.js - Nesin.io
July 21, 2023 - import fs from 'fs/promises'; const filePath = 'path/to/file' // Promise-based code fs.readFile(filePath).then(fileBuffer => { // your logic here }) // async/await code const fileBuffer = await fs.readFile(filePath); Happy reading buffers!
🌐
Medium
kgrvaidya.medium.com › attach-file-stream-buffer-in-node-js-99993280f9a4
Attach file stream / buffer in node.js | by K Ramakrishna Vaidya | Medium
August 15, 2021 - But the problem is, I was getting a ReadStream and for attaching it I needed a file object. I have gone through the stack overflow (like most of us) but couldn’t find proper answers. So after some more experimenting, I figured out a way to achieve this. Here’s the code sample to do this. const fs = require('fs') const Buffer = require('buffer').Buffer const axios = require('axios') const FormData = require('form-data')const readFromStream = () => { /* this is a sample url, can be found here.
🌐
Tabnine
tabnine.com › home › code library
Code Library - Tabnine
July 25, 2024 - Get the answers and suggestions you need from our AI code assistant. Get started in minutes with a free 90 day trial of Tabnine Pro.
🌐
GitHub
gist.github.com › 000d1a6db27e71866cfad6193ae4cf70
Browser file to Buffer · GitHub
Browser file to Buffer · Raw · fileToBuffer.js · This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters ·
🌐
Node.js
nodejs.org › api › buffer.html
Buffer | Node.js v26.10.0 Documentation
Buffer objects are used to represent a fixed-length sequence of bytes. Many Node.js APIs support Buffers. The Buffer class is a subclass of JavaScript's <Uint8Array> class and extends it with methods that cover additional use cases.
Find elsewhere
🌐
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

🌐
Stack Overflow
stackoverflow.com › questions › 19705972 › buffer-entire-file-in-memory-with-node-js
Newest Questions - Stack Overflow
I'm struggling trying to read some data from XML file with namespaces. I can load the file using simplexml_load_file() but after that I'm not able to get the Data in Row children...
🌐
Mastering JS
masteringjs.io › tutorials › node › buffer
Node.js Buffer Tutorial - Mastering JS
The most common reason for running ... true buf; // '<Buffer 7b 0a 20 20 22 6e 61 6d 65 22 ...>' Buffers have a toString() function that takes a single argument encoding....
🌐
Bun
bun.com › file system › read a file to a buffer
Read a file to a Buffer - Bun
August 7, 2026 - const path = "/path/to/package.json"; const file = Bun.file(path); const arrbuf = await file.arrayBuffer(); const buffer = Buffer.from(arrbuf);
🌐
Nesin
nesin.io › blog › write-buffer-file-nodejs
How to Write a Buffer as File in Node.js - Nesin.io
July 24, 2023 - import fs from 'fs/promises'; const filePath = 'path/to/file' const bufferData = Buffer.from('Hello World', 'utf8'); // Promise-based code fs.writeFile(filePath, bufferData) .then(() => { console.log('Buffer has been written to file successfully'); }) .catch((err) => { console.error(err); }); // Alternatively, with async/await try { await fs.writeFile(filePath, bufferData); console.log('Buffer has been written to file successfully'); } catch (err) { console.error(err); }
🌐
JavaScript in Plain English
javascript.plainenglish.io › how-to-read-files-with-buffer-stream-in-node-js-d77de6ae6b49
How to read files with Buffer & Stream in Node.js | by GP Lee | JavaScript in Plain English
July 23, 2024 - Follow to join our 3.5M+ monthly readers. ... When reading a file, Node.js allocates memory as much as the size of a file and saves the file data into the memory · Buffer indicates the memory where file data resides
🌐
Stack Overflow
stackoverflow.com › questions › 29853074 › buffer-through-file
javascript - Buffer Through File - Stack Overflow
I'm more familiar with RxJS where I probably wouldn't have much problem but I've just started switching over to highlands and would appreciate some advice. import fs from 'fs-promise' import _ from 'highland' export default function buffer(id, source) { const ctx = { fd: null, written: 0, read: 0, eof: false, err: null, path: `/tmp/${id}` } async function drain(push) { ctx.fd = ctx.fd || await fs.open(ctx.path, 'w+') const bufferSize = 1024 * 1024 while (ctx.read < ctx.written) { let buffer = new Buffer(bufferSize) buffer = buffer.slice(0, await fs.read(ctx.fd, buffer, 0, buffer.length, ctx.read)) if (buffer.length === 0) { break } ctx.read += buffer.length push(null, buffer) } } // TODO: What about errors?
🌐
DZone
dzone.com › data engineering › databases › uploading and downloading files: buffering in node.js
Uploading and Downloading Files: Buffering in Node.js
June 26, 2019 - Lines 3-12: Several variables are declared, including an array named contentBuffer which will be used to buffer the data as it streams into Node.js. Note that the file name and content type are sent from the client as HTTP headers since the body of the request is reserved for the file content.
🌐
Webdevtutor
webdevtutor.net › blog › javascript-buffer-from-file
How to Create a JavaScript Buffer from a File
To read a file into a buffer in Node.js, you can use the fs module which provides file system functionality.
🌐
W3Schools
w3schools.com › nodejs › met_buffer_from.asp
Node.js Buffer.from() Method
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP C C++ C# AWS W3.CSS HOW TO BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS DSA TYPESCRIPT ANGULAR ANGULARJS GIT POSTGRESQL MONGODB ASP AI R GO KOTLIN SWIFT SASS VUE GEN AI SCIPY CYBERSECURITY DATA SCIENCE INTRO TO PROGRAMMING INTRO TO HTML & CSS BASH RUST TOOLS · Node HOME Node Intro Node Get Started Node JS Requirements Node.js vs Browser Node Cmd Line Node V8 Engine Node Architecture Node Event Loop · Node Async Node Promises Node Async/Await Node Errors Handling · Node Modules Node ES Modules Node NPM Node package.json Node NPM Scripts Node Manage Dep Node Publish Packages · HTTP Module HTTPS Module File System (fs) Path Module OS Module URL Module Events Module Stream Module Buffer Module Crypto Module Timers Module DNS Module Assert Module Util Module Readline Module
🌐
GeeksforGeeks
geeksforgeeks.org › node.js › node-js-buffers
Node.js Buffers - GeeksforGeeks
July 12, 2025 - Example 1: Create a buffer.js file containing the following code. ... // Write JavaScript code here rbuf = new Buffer(26); var j; for (var i = 65, j = 0; i < 90, j < 26; i++, j++) { rbuf[j] = i ; } console.log( rbuf.toString('ascii'));
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › ArrayBuffer
ArrayBuffer - JavaScript - MDN Web Docs
You cannot directly manipulate the contents of an ArrayBuffer; instead, you create one of the typed array objects or a DataView object which represents the buffer in a specific format, and use that to read and write the contents of the buffer. The ArrayBuffer() constructor creates a new ArrayBuffer ...