Node JS Multer Rename Uploaded File Example

About Us:

We are from free coder. We are a group of web developers who are passionate about web development. You can learn from us php, laravel, node js, vue js, react js and many other topic on web developement.
we are very happy to help you.
free coder

Today what you are going to learn:

Hey,

This tutorial is focused on node multer rename file name. you will learn multer change filename lesson. if you want to see lesson of multer change file name then you are a right place. this lesson 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 lesson of how to change file name in multer node js. let's see solution with lesson:

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);

thank you very much for following up with me.

Tags

Recent Posts

Related Posts