3

I have file upload functionality and I am using express-fileupload module in node.js to upload file to local folder. I am explaining my existing code below.

const fileUpload = require('express-fileupload');
app.use(fileUpload({
    createParentPath: true
}));
const avatar = req.files.file;
avatar.mv('./uploads/' + avatar.name);

Here I can upload the file to my upload folder but here I need to add the timestamp with the file name and then upload so that I can differentiate if any new file is coming with same name.

2 Answers 2

5

This code may help you

const path = require('path');

let target_file = req.files.target_file;

var file_name = new Date().getTime() +'_'+target_file.name;

// target_file.mv(path, callback)
target_file.mv(path.join(__dirname, 'uploads', file_name), (err) => {
   if (err) throw err;
      res.send('File Uploaded');
})
Sign up to request clarification or add additional context in comments.

Comments

0

The code(by Elangovan Selvaraj) before this comment is absolutely correct.

I tried with:

const pdfUpload = req.files.documents;

const fileName = new Date().getTime() + '_' + pdfUpload.name;

const pdfPath = path.join(
        __dirname,
        '../public/uploads/hotels/kyc/' + `${fileName}`
      );

await pdfUpload.mv(pdfPath);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.