Search code examples
node.jsapifilestoragemulter

How to store data with multer outside of the project folder?


I want to save a form file outside of the project folder. For that I started using

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

but __dirname is already inside my project folder. Are there any methods reach out to a path out of the folder? Thank you!


Solution

  • You can either use the .. notation to go up one folder, or specify a root relative path that starts with /.

    Say you want to save your files in the folder located at /home/john/uploads and your project is in /home/john/myproject. Both of the following would work:

    destination: function (req, file, cb) {
      cb(null,  '/home/john/uploads');
      // or
      cb(null,  '../uploads');
    },