In this node js tutorial, you will learn how to upload multiple images using multer in Node.js. In this tutorial, we will use npm Multer which you will see below. We learn Node js multiple image uploads using multer, in express js Framework.
Image upload is the basic requirement of any project. So this example will guide you step by step on how to upload multiple images using multer Express js Framework. And you can understand the concept of multiple image upload easily to use.
How To Upload Multiple Image Using Multer In Node.js
Adding Multer
npm install express multer --save
const express = require('express');
const multer = require('multer');
const path = require('path');
const app = express();
const port = 3000
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(port, () => {
console.log(`Example app listening at http://localhost:${port}`)
})
Create Form
<!DOCTYPE html>
<html lang="en">
<head>
<title>How To Upload Multiple Image Using Multer In Node.js - phpcodingstuff.com</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>
<h1>Node js Express Multiple Image Upload using Multer Example - Tutsmake.com</h1>
<form action="/" enctype="multipart/form-data" method="post">
<input type="file" name="multi-files" accept='image/*' multiple>
<input type="submit" value="Upload">
</form>
</body>
</html>
Original Source: https://www.phpcodingstuff.com/blog/how-to-upload-multiple-image-using-multer-in-node-js.html
Top comments (0)