The controller calls a middleware that uses Multer disk storage engine:
var storage = multer.diskStorage({
destination: function (req, file, callback) {
callback(null, '/path/to/uploads')
},
filename: function (req, file, callback) {
callback(null, file.fieldname + '-' + Date.now())
}
});
var upload = multer({ storage: storage });
You can see that we have two options here:
– destination determines folder to store the uploaded files.
– filename determines the name of the file inside the destination folder.
Demo for upload multiple files
Open the app:
Choose the images to upload:
Click on Submit button, if the process is successful, you can see the files in upload folder:
If the number of files we choose is larger than 10 (I will show you how to set the limit later):
Practice
Now this is the time for building our app.
Project Structure
Look at the project structure:
You can see that we have these main parts:
– upload: the folder for storing uploaded files.
– views/index.html: contains HTML form for user to upload files.
– routes/web.js: defines routes for endpoints that is called from views, use controllers to handle requests.
– controllers: home.js to show views/index.html, upload.js to handle upload multiple files with middleware.
– middleware/upload.js: initializes Multer disk storage engine.
– server.js: initializes routes, runs Express app.
Setup Node.js modules
We need to install 2 packages: express and multer.
So run the command:
npm install express multer
The script above helps us show preview of the chosen files. To make things simple, I don’t explain it deeply in the tutorial.
Create middleware for upload multiple files
Inside middleware folder, create upload.js:
const util = require("util");
const path = require("path");
const multer = require("multer");
var storage = multer.diskStorage({
destination: (req, file, callback) => {
callback(null, path.join(`${__dirname}/../../upload`));
},
filename: (req, file, callback) => {
const match = ["image/png", "image/jpeg"];
if (match.indexOf(file.mimetype) === -1) {
var message = `${file.originalname} is invalid. Only accept png/jpeg.`;
return callback(message, null);
}
var filename = `${Date.now()}-bezkoder-${file.originalname}`;
callback(null, filename);
}
});
var uploadFiles = multer({ storage: storage }).array("multi-files", 10);
var uploadFilesMiddleware = util.promisify(uploadFiles);
module.exports = uploadFilesMiddleware;
– We define a storage configuration object, then use multer module to initialize middleware and util.promisify() to make the exported middleware object can be used with async-await.
– To limit the number of files to upload each time, we use array() function with the first parameter is the name of input tag (in html view: <input type="file" name="multi-files">), the second parameter is the max number of files.
– We also check if the file is an image or not using file.mimetype. Then we add the [timestamp]-bezkoder- prefix to the file’s original name to make sure that the duplicates never occur.