Node JS Multer Rename Uploaded File Example
Hey,
This tutorial is focused on node multer rename file name. you will learn multer change filename example. if you want to see example of multer change file name then you are a right place. this example will help you node js change file name uploaded. Follow bellow tutorial step of how to rename filename multer nodejs.
I will give you simple solution with full example of how to change file name in multer node js. let's see solution with example:
Solution:
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 })
Example:
const express = require('express');
const multer = require('multer');
const path = require('path');
const fs = require('fs');
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.single('image'),(req, res) => {
const { filename: image } = req.file;
res.redirect('/');
});
app.listen(3000);
I hope it can help you...
Hardik Savani
I'm a full-stack developer, entrepreneur and owner of ItSolutionstuff.com. I live in India and I love to write tutorials and tips that can help to other artisan. I am a big fan of PHP, Laravel, Angular, Vue, Node, Javascript, JQuery, Codeigniter and Bootstrap from the early stage. I believe in Hardworking and Consistency.
We are Recommending you
- Node JS Resize Image Before Upload using Multer Sharp
- Multiple File Upload in Node JS using Multer Example
- Node js Express Multiple Image Upload using Multer Example
- File Upload in Node JS using Multer Example
- Node js Express Image Upload using Multer Example
- Four Reasons Why Node.js is your Server-side Hero
- How to Connect MySQL Database in Node JS?
- Laravel Authenticate User in NodeJS with Socket io using JWT
- How to Upgrade Node.js Version in Ubuntu?
- How to Use Foreach Object in Node.js?