简体   繁体   中英

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!

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');
},

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM