When examining the buffer property of each uploaded file, particularly when dealing with multiple files

I'm encountering an issue while trying to upload multiple files to my server using multer. Although I can access the req.files array, I am unable to retrieve the buffer property of the files. Upon attempting to log them with console.log, all I get is undefined.

This snippet shows my HTML (ejs) code:

<form method="post" action="/send" enctype="multipart/form-data">
    <div class="row">
        <div class="col">
            <input type="file" multiple class="form-control" name="files">
        </div>
        <div class="col text-end">
            <input type="submit" class="btn btn-primary w-100">
        </div>
    </div>
</form>

The route definition looks like this:

const express = require("express");
const router = express.Router();

const multer = require("multer");
const upload = multer({ dest: "uploads/" });

const indexController = require("../controllers/index.controller");

router.post("/send", upload.array("files", 5), indexController.send);

module.exports = router;

... now onto the controller:

exports.send = async (req, res) => {
    ...
    console.log(req.files); // [ { fieldname: 'files', ..., size: 1576 } ]
    console.log(req.files.map((f) => f.buffer)); // [ undefined ]
    ...
}

Any suggestions on how to access the .buffer property of each file when dealing with multiple uploads? Your assistance is greatly appreciated.

Answer №1

Even though I am utilizing memoryStorage, I am still struggling to resolve this issue. The buffer file is not being obtained in the Multers filter option; it only appears at the controllers level. Despite attempting the following code, I am only receiving these fields without the buffer:

// OUTPUT (buffer not included)

{
    fieldname: 'formImages',
    originalname: 'file-test.jpeg',
    encoding: '7bit',
    mimetype: 'image/jpeg'
}

// CODE:

const multer = require("multer");
const sharp = require("sharp");
const sharpFilter = async (req, file, cb) => {
  const locationPath = `${process.env.UPLOAD_FOLDER + "/" + file.originalname}`;
  await sharp(file)
    .resize({
      width: 2000,
      height: 2000,
      fit: "cover",
    })
    .toFile(locationPath);
  cb(null, true);
};

exports.upload = multer({
  storage: multer.memoryStorage(),
  fileFilter: sharpFilter,
});

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Synchronize display with data loading

My plan involves executing 2 AJAX calls: Loading a partial HTML template. Fetching JSON data for the template and displaying it within the loaded template. The JSON must be retrieved separately from the template because users might initiate a "refresh" ...

Issue with installing vscode-ripgrep during VSCode build/run process

After attempting to build and run VSCode on my Ubuntu 17.10 by following the instructions from this guide: https://github.com/Microsoft/vscode/wiki/How-to-Contribute#build-and-run, I encountered an issue when installing dependencies using yarn. The error m ...

Get rid of the folder from the URL using an <a> tag

I have both an English and French version of my website located at: *website.com/fr/index.php *website.com/index.php Currently, I have a direct link to switch between the two versions: -website.com/fr/index.php -website.com/index.php. However, I ...

Revealing and concealing adjacent elements within a specified class

In an attempt to create a carousel that functions by hiding and showing images when the next and previous buttons are clicked, I have organized my images in a table and assigned the li elements a class of 'li'. There are four images in total, wit ...

Angular fails to include the values of request headers in its requests

Using Django REST framework for the backend, I am attempting to authenticate requests in Angular by including a token in the request headers. However, Angular does not seem to be sending any header values. Despite trying various methods to add headers to ...

Extracting information from JSON using jQuery

this is a sample json object: { "box 1": [{ "difficulty_grade": "5a+" }, { "gym": "New_gym" }, { "route_author": "some author" },]} https://i.sstatic.net/UJodJ.png Here is the code snippet: variable groups contains JSON data as shown in the ...

Guide to setting up a dropdown menu with Material UI in React JS

I'm currently working on a dropdown menu that includes a nested menu structure, as depicted below: https://i.sstatic.net/FleC5.png Upon expanding the dropdown, two options are displayed: https://i.sstatic.net/jQlwN.png The issue I'm facing is ...

Having trouble with my React input field - once I set the value, it becomes immutable

After setting the value of my state component, my React input component becomes uneditable state = {text: ''} return( <input id="search" type="text" value={this.s ...

Issue encountered while attempting to load a JSON file into an ExtJS JSONPStore due to a

After validating a json file with json.bloople.net, the file begins as follows: { "sepString": "--", When attempting to load this json file into an ExtJS JsonPStore, Chrome displays the error: Uncaught SyntaxError: Unexpected token : in line 2 What ...

The Facebook app is experiencing issues on Chrome, yet is functioning properly on Firefox

Lately, I've ventured into the world of creating Facebook Apps on heroku. After creating a test app and uploading a page with HTML5, CSS, and Javascript, I encountered an issue where the app wasn't displaying correctly in Google Chrome , but work ...

Combining PouchDB with Vue.js for seamless integration

Has anyone successfully integrated PouchDB / vue-pouch-db into a Vue.js application before? I encountered an error when attempting to define the PouchDB database. Here are the definitions I tried: import PouchDB from 'pouchDB' or import PouchDB ...

Steps for repairing the encoding of a string in JavaScript

I have encountered a broken string from another software source and I am attempting to repair its encoding using JavaScript, but I seem to be missing a crucial step. Here is an example of the broken string: Détecté à lors ô ù The desir ...

turning off next.js server side rendering in order to avoid potential window is undefined issues

I am currently managing a private NPM package that is utilized in my Next.js project, both of which are React and Typescript based. Recently, I integrated a graph feature into the NPM package and encountered an issue where any reference to window within t ...

The ts-loader seems to be malfunctioning (It appears that a suitable loader is required to handle this file type, as no loaders are currently set up to process it)

I'm currently in the process of integrating TypeScript into a JavaScript project, but it seems like webpack is not recognizing the ts-loader for files with the .tsx extension. I've attempted to use babel and even tried awesome-ts-loader, but none ...

Creating a dynamic table accordion with PHP and MySQL

I am currently working on creating an accordion table to display data retrieved from a database. I want the description data to be shown in a row below when clicking on the respective row. Despite my efforts in modifying various code snippets that I have c ...

Sliding Image Menu using jQuery

I am struggling with creating a menu using jquery mouseenter / mouseout effects. My goal is to have a small icon displayed that expands to the left and reveals the menu link when a user hovers over it. The issue I am facing is that the effect only works w ...

Implementing authentication fallback during re-login when a session expires in a web application built with React, Node.js, and Mariadb database

Greetings everyone, this is my debut post here so kindly bear with me :D. Currently, I am in the process of developing a social app where I am incorporating a fallback mechanism for situations when my database goes offline. Within my Login.jsx component, I ...

secure user authentication using express and sockjs

Is there a resource that explains methods for authenticating users in SockJS without using cookies? Ideally, the information should include examples. I prefer not to use Socket.IO. ...

Using jQuery to create a dynamically moving div that forms an endless wall

In order to create a continuous wall that pulls text from a database and displays it, I've developed the following code: $(function() { var x = 0; var y = 100.0; var z=0; setInterval(function() { x -= 0.1; y -= 0.1; ...

Having trouble displaying an array in Handlebars on a Node.js and Express server?

I am facing a challenge with handlebars that has me feeling stuck. This is my second day attempting to solve it, but I seem to be hitting a roadblock. My goal is to incorporate handlebars for the first time in order to add some dynamism to the web page I ...