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

191
Vistas
cant save a file after uploading multer
<form action="/upload" method="post">
<input type="file" name="avatar" id="avatarinput" onchange="readfichier()"> 
<button type='submit'>Save</button>
</form>

SERVER

app.post('/upload', upload.single('avatar'), function (req, res, next) {
    console.log(req.file);

    fs.rename(req.file.path, 'uploads/'+req.file.originalname, function (err) {
         if (err) throw err;
          console.log('renamed complete');
    });

    res.end('Success');
  // req.file is the `avatar` file
  // req.body will hold the text fields, if there were any
})

I got this error: TypeError: Cannot read properties of undefined (reading 'path')

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

0

First, you need to use a disk storage engine to take full control on storing files to disk.

const storage = multer.diskStorage({
  destination: function (req, file, cb) {
    cb(null, './uploads')
  },
  filename: function (req, file, cb) {
    cb(null,file.fieldname+'-'+Date.now()+'.'+file.mimetype.split('/').reverse()[0]);
  }
})

const upload = multer({ storage: storage })

Now, define your route and add multer as a middleware.

app.post('/upload', upload.single('avatar'), function (req, res, next) {
    console.log(req.file);

    fs.rename(req.file.path, 'uploads/'+req.file.originalname, function (err) {
         if (err) throw err;
          console.log('renamed complete');
    });

    res.end('Success');
})

This much is described very well in previous answers as well. But the thing you're missing is to add enctype="multipart/form-data" into your HTML form. Read more about why we need to do this here.

<form action="/upload" method="post" enctype="multipart/form-data">
<input type="file" name="avatar" id="avatarinput" onchange="readfichier()"> 
<button type='submit'>Save</button>
</form>

I hope this solves all your issues. If not, refer to this answer.

about 4 years ago · Juan Pablo Isaza Denunciar

0

I guess this is the only code you have.

first you have to define multer as middleware of an your api

then you have to told your multer that where you want to store your image
I guess it's in your disk

so on your server code looks like this

var multer = require('multer');  
var storage = multer.diskStorage({  
   destination: function (req,file,cb){  

       cb(null, './uploads')  
    },
    filename: function (req,file,cb){
        cb(null,file.fieldname+'-'+Date.now()+'.'+file.mimetype.split('/').reverse()[0]);
    },
});

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



app.post('/upload', upload.single('avatar'), function (req, res, next) {
    console.log(req.file);

    fs.rename(req.file.path, 'uploads/'+req.file.originalname, function (err) {
         if (err) throw err;
          console.log('renamed complete');
    });

    res.end('Success');
  // req.file is the `avatar` file
  // req.body will hold the text fields, if there were any
})
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