We will learn how to upload Multiple File in Node JS using Multer Example
Step 1:
Create Node App
mkdir my-app
cd my-app
npm init
Step 2:
Install express and multer
npm install express multer --save
Step 3:
Create app.js file app.js
const express = require('express');
const multer = require('multer');
const path = require('path');
const app = express();
const storage = multer.diskStorage({
destination: function(req, file, cb) {
cb(null, 'uploads/');
},
filename: function(req, file, cb) {
cb(null, file.fieldname + '-' + Date.now() + path.extname(file.originalname));
}
});
var upload = multer({ storage: storage })
app.get('/', (req, res) => {
res.sendFile(__dirname + '/index.html');
});
app.post('/', upload.array('multi-files'), (req, res) => {
res.redirect('/');
});
app.listen(3000);
Step 4:
Create index.html file index.html
<!DOCTYPE html>
<html lang="en">
<head>
<title>Node js Express Multiple File Upload using Multer Example - rathorji.in</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>
<h1>Node js Express Multiple File Upload using Multer Example - rathorji.in</h1>
<form action="/" enctype="multipart/form-data" method="post">
<input type="file" name="multi-files" multiple>
<input type="submit" value="Upload">
</form>
</body>
</html>
run by following command:
npm start
May this example help you