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

264
Views
How to upload single file, and array of files with different name in multer?

I want to upload main image, and side images from 2 different file inputs using multer, but I couldn't find how to do that, multer seems to only accept one upload on route, so how can I do that? Multer is throwing me unexpected field error in my code.

HTML Form:

<form method="POST" action="/addprod" enctype="multipart/form-data">
    <h3>Add product</h3>
    <h4>Main image</h4>
    <input type="file" class="mainimg" accept="image/*" name="mainimg">
    <h4>Side images</h4>
    <input type="file" class="sideImgs" accept="image/*" multiple name="sideimgs">
    <button>Add product</button>
</form>

node.js

const storage = multer.diskStorage({
    destination: function(req, file, cb) {
        cb(null,'../public/prodImgs/')
    },
    filename: function(req, file, cb) {
        const newName = Date.now() + '-' + Math.round(Math.random() * 100)
        cb(null,file.fieldname + '-' + newName)
    }
});
const upload = multer({ storage: storage });
    
let mid = {
    main: upload.single('mainimg'),
    sides: upload.array('sideimgs')
}

app.post('/addprod', [mid.main, mid.sides], async (req, res) =>{
    console.log(req.files,"\n\n\n")
    console.log(req.body)
});
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

I find a solution in here.Like so:

app.post('/addprod', upload.fields([{
    name: 'mainimg', maxCount: 1
  }, {
    name: 'sideimgs', maxCount: 5
  }]), async (req, res) =>{
    console.log(req.files,"\n\n\n")
    console.log(req.body)
})
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!