How to upload multiple files in Node.js
Handling multiple file uploads simultaneously is crucial for Node.js applications with features like photo galleries, document management, or batch processing. As the creator of CoreUI with over 11 years of Node.js development experience since 2014, I’ve built multi-file upload systems for enterprise document management platforms. The most effective solution is to use Multer’s array() or fields() methods to handle multiple files in a single request. This approach efficiently processes multiple files while maintaining validation and storage control.
Use Multer’s array() method to handle multiple file uploads in Node.js.
const express = require('express')
const multer = require('multer')
const path = require('path')
const app = express()
const storage = multer.diskStorage({
destination: (req, file, cb) => {
cb(null, 'uploads/')
},
filename: (req, file, cb) => {
const uniqueSuffix = Date.now() + '-' + Math.round(Math.random() * 1E9)
cb(null, file.fieldname + '-' + uniqueSuffix + path.extname(file.originalname))
}
})
const upload = multer({
storage: storage,
limits: { fileSize: 5 * 1024 * 1024 }
})
app.post('/upload-multiple', upload.array('files', 5), (req, res) => {
if (!req.files || req.files.length === 0) {
return res.status(400).json({ error: 'No files uploaded' })
}
const uploadedFiles = req.files.map(file => ({
filename: file.filename,
originalname: file.originalname,
size: file.size,
path: file.path
}))
res.json({
message: `${req.files.length} files uploaded successfully`,
files: uploadedFiles
})
})
The upload.array('files', 5) middleware accepts up to 5 files from the ‘files’ field. Uploaded files are available in req.files as an array. Each file object contains metadata like filename, size, and path. You can map through the array to extract relevant information or process each file individually. The second parameter in array() sets the maximum number of files allowed, providing protection against abuse.
Best Practice Note
This is the same multi-file upload approach we use in CoreUI backend systems for batch processing. Always set a reasonable file count limit and validate each file individually. For different file types from different fields, use upload.fields([{name: 'photos', maxCount: 5}, {name: 'documents', maxCount: 3}]) to handle mixed uploads with specific constraints.



