Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

148
Visualizações
Reading the buffer property of each uploaded file, when there are multiple

I am trying to upload multiple files to my server using multer. Even though I can read the req.files array, but can't access the buffer property of them. I tried console.logging them, which only results in undefined.

This is 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:

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;

... the controller:

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

How do I read the .buffer property of each file, when there are multiple? Any help is appreciated.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

I am using memoryStorage and still cant figure this out. I wont get the buffer file in the Multers filter option, only at the controllers level. I tried this code but I only get these fields (no buffer)

// RESPONSE (buffer is missing)

{
    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,
});
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda