What if you could let users upload dozens of files at once without extra code hassle?
Why Multiple file uploads in Express? - Purpose & Use Cases
Start learning this pattern below
Jump into concepts and practice - no test required
Imagine you have a website where users can upload several photos at once, but you try to handle each file upload separately with basic HTML forms and server code.
Handling each file one by one manually means writing repetitive code, managing multiple requests, and risking errors like missing files or server crashes when many files come in at once.
Using multiple file upload support in Express lets you handle all files in one go, simplifying your code and making uploads faster and more reliable.
app.post('/upload', (req, res) => { const file1 = req.files.file1; const file2 = req.files.file2; /* handle each file separately */ });app.post('/upload', upload.array('photos', 5), (req, res) => { const files = req.files; /* handle all files together */ });
You can easily accept many files at once, improving user experience and reducing server-side complexity.
Think of a social media app where users want to share multiple pictures from their phone gallery in one post without waiting for each to upload separately.
Manual file uploads are slow and error-prone when handling many files.
Express multiple file upload support simplifies code and improves reliability.
Users get a smoother experience uploading many files at once.
Practice
Solution
Step 1: Understand multer upload methods
Multer provides different methods:single()for one file,array()for multiple files, andfields()for multiple fields.Step 2: Identify method for multiple files
To upload multiple files under the same field name,upload.array()is used.Final Answer:
upload.array() -> Option AQuick Check:
Multiple files = upload.array() [OK]
- Using upload.single() for multiple files
- Confusing upload.fields() with upload.array()
- Not specifying max count in upload.array()
Solution
Step 1: Recall multer syntax for multiple files
The methodupload.array(fieldname, maxCount)accepts multiple files with the same field name and limits the count.Step 2: Match syntax to question
To accept up to 5 files named 'photos',upload.array('photos', 5)is correct.Final Answer:
upload.array('photos', 5) -> Option AQuick Check:
Max 5 files = upload.array('photos', 5) [OK]
- Using upload.single() with max count
- Confusing upload.fields() syntax
- Using upload.none() incorrectly
app.post('/upload', upload.array('docs', 3), (req, res) => {
res.send(req.files.length);
});What will be the response if a user uploads 2 files named 'docs'?
Solution
Step 1: Understand upload.array behavior
The middlewareupload.array('docs', 3)allows up to 3 files. If user uploads 2 files, both are accepted.Step 2: Check response code
The handler sendsreq.files.length, which is the number of uploaded files, here 2.Final Answer:
2 -> Option DQuick Check:
Uploaded files count = 2 [OK]
- Assuming maxCount is always returned
- Confusing req.file with req.files
- Expecting error if fewer than maxCount files uploaded
app.post('/upload', upload.array('files'), (req, res) => {
console.log(req.file);
res.send('Uploaded');
});Solution
Step 1: Check multer usage for multiple files
When usingupload.array(), uploaded files are stored inreq.files(an array), notreq.file.Step 2: Identify error in code
The code logsreq.file, which will be undefined or incorrect for multiple files.Final Answer:
Should use req.files instead of req.file to access multiple files -> Option CQuick Check:
Multiple files = req.files [OK]
- Forgetting req.files vs req.file difference
- Assuming maxCount is mandatory
- Thinking upload.array() is invalid
Solution
Step 1: Understand multer methods for multiple fields
To handle multiple fields with multiple files each, useupload.fields()with an array of objects specifying field names and max counts.Step 2: Analyze options
upload.fields([{ name: 'images', maxCount: 3 }, { name: 'documents', maxCount: 2 }]) correctly usesupload.fields([{ name: 'images', maxCount: 3 }, { name: 'documents', maxCount: 2 }]). upload.array('images', 3), upload.array('documents', 2) is invalid syntax as you cannot chain two upload.array() calls in one route.Final Answer:
upload.fields([{ name: 'images', maxCount: 3 }, { name: 'documents', maxCount: 2 }]) -> Option BQuick Check:
Multiple fields = upload.fields() [OK]
- Trying to chain multiple upload.array() calls
- Using upload.single() for multiple files
- Using upload.none() which disables file uploads
