Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

186
Views
no puedo guardar un archivo después de cargar multer
<form action="/upload" method="post"> <input type="file" name="avatar" id="avatarinput" onchange="readfichier()"> <button type='submit'>Save</button> </form>

SERVIDOR

 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 })

Recibí este error: TypeError: no se pueden leer las propiedades de undefined (leyendo 'ruta')

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Primero, debe usar un motor de almacenamiento en disco para tomar el control total sobre el almacenamiento de archivos en el disco.

 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 })

Ahora, defina su ruta y agregue multer como un 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'); })

Esto también se describe muy bien en respuestas anteriores. Pero lo que te falta es agregar enctype="multipart/form-data" en tu formulario HTML. Lea más acerca de por qué necesitamos hacer esto aquí .

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

Espero que esto resuelva todos tus problemas. Si no, consulte esta respuesta .

about 4 years ago · Juan Pablo Isaza Report

0

Supongo que este es el único código que tienes.

primero tienes que definir multer como middleware de tu api

entonces tienes que decirle a tu multer dónde quieres almacenar tu imagen
Supongo que está en tu disco.

entonces en el código de su servidor se ve así

 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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!