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

290
Vistas
Multer doesn't save the file

Multer does not save the file and I cannot see any logs... The folder is empty, no any files.

The image data is actually sent to the server as its console logged: Screenshot of req.files from log

What am I doing wrong? Please help me find

const express = require('express');
const router = express.Router();
const path = require('path');
const multer = require('multer');

const storage = multer.diskStorage({
  destination: (req, file, cb) => {
    console.log('destination') // can't see this log
    cb(null, './uploads/');
  },
  filename: (req, file, cb) => {
    console.log('filename') // can't see this log
    const ext = file.mimetype.split("/")[1];
    cb(null, Date.now() + path.extname(file.originalname));
  }
});

const uploadImage = multer({
  storage,
  limits: { fieldSize: 1024 * 1024 },
  fileFilter: (req, file, cb) => {
    console.log('log!')  // can't see this log
    const ext = path.extname(file.originalname);
    if (ext !== '.jpg' && ext !== '.jpeg' && ext !== '.png') {
      console.log('not log!')  // can't see this log
      const err = new Error('Extention');
      err.code = 'EXTENTION';
      return cb(err);
    }
    console.log('log!') // can't see this log
    cb(null, true);
  }
}).single('photo');

router.post('/image', async (req, res) => {
  console.log(req.files) // can see this log

  uploadImage(req, res, err => {
    console.log('upload') // can see this log

    let error = '';
    if (err) {
      console.log(err)
      if (err.code === 'LIMIT_FILE_SIZE') {
        error = 'Max 500kb!';
      }
      if (err.code === 'EXTENTION') {
        error = 'Only jpg and png!';
      }
    }

    res.json({
      ok: !error,
      error
    });
  })
});
about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

Multer finally gave an error, so the problem was resolved... The problem was that the names of these fields are different:

Server: .single('pic');

And in the request to the server: formData.append('pic', file);

about 4 years ago · Juan Pablo Isaza Denunciar

0

The code seems to work fine for me but I just had to manually create the uploads directory.

I used postman to test this and the picture appeared in my uploads directory.

# Console Output
SERVER RUNNING ON 3000
undefined
log!
log!
destination
filename
upload
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