Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

69
Vistas
Disable automatic upload with Multer

I'm using multer for the first time and I noticed the image is always uploaded.

Now my problem is that I would like to do some data validation on the body of the request with Joi, then move the file in the appropriate folder if the data validation is a success.

Is it possible with Multer and how would you achieve this ?

Or, is there a way to disable the automatic upload with Multer and move the file manually once the data validation is done ?

Thanks a lot for your help.

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

There are multiple ways to do this. You could use a custom middleware doing validation before Multer, or you could manage the validation within Multer directly. Here is an example using the default DiskStorage:

const storage = multer.diskStorage({
  destination: function (req, file, cb) {
    const valid = ...; // your validation logic
    if (valid) cb(null, '/tmp/my-uploads');
    else cb(new Error('Validation failed.'));
  },
});

const upload = multer({ storage: storage });

If you need more control, you can use MemoryStorage:

const storage = multer.memoryStorage();
const upload = multer({ storage: storage });

app.post('/xyz', upload.single('file'), (req, res) => {
  // because of memoryStorage, the file will be available in a buffer
  // do your validation logic here, and when you are happy write the buffer to the disk
});

Read more about storage in the docs.

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda