Back to results for “Express & Middleware”
Node.js › File Uploads & Media Processing

What is the difference between upload.single(), upload.array(), and upload.fields() in multer?

Intermediate

Answer

upload.single(fieldName) handles exactly one uploaded file under a specific form field name, populating req.file. upload.array(fieldName, maxCount) handles multiple files uploaded under the same field name (like several photos), populating req.files as an array. upload.fields([{ name, maxCount }, ...]) handles multiple distinct named fields, each potentially with its own file(s), useful when a form has several different types of file inputs (like a resume and a cover letter uploaded together).
app.post('/single', upload.single('avatar'), handler); // req.file
app.post('/multiple', upload.array('photos', 10), handler); // req.files (array)
app.post('/mixed', upload.fields([
  { name: 'resume', maxCount: 1 },
  { name: 'coverLetter', maxCount: 1 },
]), handler); // req.files.resume, req.files.coverLetter
Real-world example A job-application form uses upload.fields() to accept both a resume and a cover letter as two distinctly named file inputs in a single form submission, letting the handler access each one separately via req.files.resume and req.files.coverLetter.

Common follow-ups: What happens if a client attempts to upload more files than the maxCount limit specified for upload.array()?;How would you handle a form with a mix of both regular text fields and file uploads together?