Uploading multiple files lets users send many files at once. This saves time and makes apps easier to use.
Multiple file uploads in Express
Start learning this pattern below
Jump into concepts and practice - no test required
or
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Introduction
Syntax
Express
const multer = require('multer'); const upload = multer({ dest: 'uploads/' }); app.post('/upload', upload.array('files', 10), (req, res) => { // req.files contains array of uploaded files res.send('Files uploaded'); });
upload.array(fieldname, maxCount) handles multiple files from the same field.
The req.files array holds info about each uploaded file.
Examples
Express
app.post('/photos/upload', upload.array('photos', 5), (req, res) => { res.send(`Uploaded ${req.files.length} photos.`); });
Express
app.post('/docs/upload', upload.array('documents'), (req, res) => { res.send(`Received ${req.files.length} documents.`); });
Sample Program
This Express app uses multer to accept up to 3 files from the 'files' field. It responds with how many files were uploaded or an error if none.
Express
const express = require('express'); const multer = require('multer'); const app = express(); const upload = multer({ dest: 'uploads/' }); app.post('/upload', upload.array('files', 3), (req, res) => { if (!req.files || req.files.length === 0) { return res.status(400).send('No files uploaded.'); } res.send(`Uploaded ${req.files.length} files.`); }); app.listen(3000, () => { console.log('Server running on http://localhost:3000'); });
Important Notes
Make sure the HTML form uses enctype="multipart/form-data" to send files correctly.
Files are saved temporarily in the 'uploads/' folder; you can customize storage options.
Always validate file types and sizes for security.
Summary
Use upload.array() from multer to handle multiple file uploads in Express.
The uploaded files are available as an array in req.files.
Set a max count to limit how many files users can upload at once.
Practice
1. In Express, which method from multer is used to handle multiple file uploads at once?
easy
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]
Hint: Multiple files use upload.array() method in multer [OK]
Common Mistakes:
- Using upload.single() for multiple files
- Confusing upload.fields() with upload.array()
- Not specifying max count in upload.array()
2. Which of the following is the correct syntax to accept up to 5 files named 'photos' using multer in Express?
easy
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]
Hint: Use upload.array('fieldname', maxCount) for multiple files [OK]
Common Mistakes:
- Using upload.single() with max count
- Confusing upload.fields() syntax
- Using upload.none() incorrectly
3. Given this Express route using multer:
What will be the response if a user uploads 2 files named 'docs'?
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'?
medium
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]
Hint: req.files.length shows how many files were uploaded [OK]
Common Mistakes:
- Assuming maxCount is always returned
- Confusing req.file with req.files
- Expecting error if fewer than maxCount files uploaded
4. What is wrong with this Express route for multiple file uploads?
app.post('/upload', upload.array('files'), (req, res) => {
console.log(req.file);
res.send('Uploaded');
});medium
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]
Hint: Use req.files (plural) for multiple files, not req.file [OK]
Common Mistakes:
- Forgetting req.files vs req.file difference
- Assuming maxCount is mandatory
- Thinking upload.array() is invalid
5. You want to accept multiple files from two different fields: 'images' (max 3 files) and 'documents' (max 2 files). Which multer setup correctly handles this?
hard
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]
Hint: Use upload.fields() for multiple fields with multiple files [OK]
Common Mistakes:
- Trying to chain multiple upload.array() calls
- Using upload.single() for multiple files
- Using upload.none() which disables file uploads
