Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

216
Views
React and NodeJS upload file with Multer can`t set filename and termination

I am building an app in React and Node that has an upload component. I managed to upload the file in React sent it to node with multer but it gets saved as a random string without any termination, just of type file. If i manually change the termination to pdf to say I can open it and see the content but I would like to do that automatically.

Here is what I have done in nodejs:

var multer = require('multer')
const upload = multer({ dest: "./uploads" });

app.post('/upload', upload.single('file'), function (req, res, next) {


var f = req.file;
console.log(f);
var s = req.body.standard;
console.log(s);




})

On he file console log it prints me the details of the file. Of the things I have tried was to change req.file.filename = req.file.originalname; req.file.path = "./uploads/"+req.file.originalname;

thinking that it might change the file name on the computer but it didn`t work.

Another thing that I have tried is to insert this code:

var storage = multer.diskStorage({
  destination: function (req, file, cb) {
  cb(null, 'public')
},
filename: function (req, file, cb) {
  cb(null, Date.now() + '-' +file.originalname )
}
})

and modify const upload with var upload = multer({ storage: storage }).single('file')

outside the post method and inside it but it does not work. It does not do anything.

Can someone please provide me full code on how my js page sould look with the post method and the code that sets the filename and folder inside the node project. Thanks in advance. On the internet I can only find snippets of code and I do not know where to integrate them.

Thanks

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Try bellow code

const multer = require('multer')

const storage = multer.diskStorage({
    destination: function(req, file, cb) {
        cb(null, 'public')
    },
    filename: function(req, file, cb) {
        cb(null, Date.now() + '-' + file.originalname)
    }
})

const upload = multer({
    storage: storage
})

app.post('/upload', upload.single('file'), function(req, res, next) {
    const f = req.file;
    console.log(f);
    const s = req.body.standard;
    console.log(s);
})
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!