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

252
Views
En la extensión de archivo multer que no debería guardarse para guardarse

TAREA DE CÓDIGO: El código que le dice a multer que guarde el archivo con extensión pdf

PROBLEMA: recibo un error de respuesta, pero el archivo se guarda dentro de la carpeta

 const express = require("express"); const app = new express(); const multer = require("multer"); const upload = multer({ dest: "images", // destination to save the image limits: 100000, // limiting the size of file to 1mb and 1 mb = 100000bytes fileFilter(req, file, cb) { if (!file.originalname.endsWith("pdf")) { cb(new Error("please upload PDF file extension")); // sending error } cb(undefined, true); // 3 type of call backs // cb(new Error('please upload PDF file extension'));// sending error // cb(undefined,true)// undefined means no error and true means accepting file // cb(undefined,false)// undefined means no error and true means rejecting file }, }); app.post("/upload", upload.single("upload"), (req, res) => { res.send(); }); app.listen(8000, () => { console.log("server fired off"); });

El mensaje de error es correcto según lo que quiero ingresar la descripción de la imagen aquí Pero el archivo se guarda en la carpeta de imágenes que no debe guardarse porque estoy enviando la extensión jpg

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

0

Parece que el problema es que la ejecución del código continúa después de cb(new Error(..) , por lo que también se llama a cb(undefined,true) , diciéndole a multer que todo está bien. Cámbielo a:

 if (!file.originalname.toLowerCase().endsWith("pdf")) { return cb(new Error("please upload PDF file extension")); // sending error } cb(undefined, true);

Tenga en cuenta que también usé .toLowerCase() solo para asegurarme de que se carguen los archivos con una extensión .PDF .

about 4 years ago · Juan Pablo Isaza Report

0

Estoy haciendo esta nueva función llamada checkFileType()

 function checkFileType(file, cb){ const filetypes = /pdf/; const extname = filetypes.test(file.originalname.split('.')[file.originalname.split('.').length-1]); const mimetype = filetypes.test(file.mimetype); if(mimetype && extname){ return cb(null,true); }else{ cb(error = 'message : please upload PDF file extension'); } }

Y lo implementé así.

 const upload = multer({ dest: "images", // destination to save the image limits: 100000, // limiting the size of file to 1mb and 1 mb = 100000bytes fileFilter(req, file, cb) { checkFileType(file, cb); }, });
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!