question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Can I set the dest (upload folder) dynamically?

See original GitHub issue

I’d like to upload files to different folder based on user id.

My code is:

const multer = require('multer')
const upload = multer({ dest: 'uploads/'})

app.get('/', function (req, res) {
  res.send('Image Extraction Server');
})

app.post('/upload', upload.array('photos',100), (req,res) => {   
  // get the user id from req
  // how can I change the dest path? 
  res.send('{"status":"ok"}')
})


Is this possible? Thanks.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:16 (2 by maintainers)

github_iconTop GitHub Comments

69reactions
gireeshpunathilcommented, Oct 31, 2019

pasting the textual version of the same, so that people can copy-paste easily

const multer = require('multer')
const storage = multer.diskStorage({
destination: (req, file, cb) => {
  const { userId } = req.body
  const dir = ./uploads/${userId}
  fs.exists(dir, exist => {
  if (!exist) {
    return fs.mkdir(dir, error => cb(error, dir))
  }
  return cb(null, dir)
  })
},
filename: (req, file, cb) => {
  const { userId } = req.body
  cb(null, UserId-${userId}-Image-${Date.now()}.png)
}
})

const upload = multer({ storage })
38reactions
kuubsoncommented, Oct 31, 2019

image

Read more comments on GitHub >

github_iconTop Results From Across the Web

multer: dynamic destination path - node.js - Stack Overflow
6 Answers 6 · 1.Store the file first in a common directory, like /tmp/. · 2.Copy/move the file anywhere you want, to CDN...
Read more >
Upload different files in different folders using Multer in NodeJs
Case 1: When you only want to upload files in different folders on the basis of the field name. var upload = multer({storage:assign});...
Read more >
input to dynamically set the destination folder (Page 1)
I'm using "multipart_params" to pass my variable to upload.php's file. If variable is static, it works fine, but if variable's dynamic, nothing ......
Read more >
dynamic change of the upload folder - WordPress.org
I want to change the upload folder dynamically but I just can't find the solution. How can i change dynamic the folder /CUSTOM....
Read more >
Multer create dynamic folder
multer(opts) Multer accepts an options object, the most basic of which is the dest property, which tells Multer where to upload the files....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found