Search code examples
node.jsexpressmulter

Node: multer change destination folder for fields


I have simple multer image uploading.

// Multer settings
// STORAGE FOR USER AVATAR
var storageAvatar = multer.diskStorage({
  destination: function (req, file, cb) {
    cb(null, 'public/uploads/avatars/')
  },
  filename: function (req, file, cb) {
    cb(null, req.user.id + '.jpg')
  }
})
 // STORAGE FOR ARTICLE THUMBNAILS
var storageThumbnail = multer.diskStorage({
  destination: function (req, file, cb) {
    cb(null, 'public/uploads/thumbnails/')
  },
  filename: function (req, file, cb) {
    cb(null, "clanok" + '.jpg')
  }
})

// SETTING UPLOAD FOLDER
var upload = multer({
  storage: storageAvatar
})

// Multer BEFORE CSRF!!!
app.use(upload.fields([{
  name: 'avatar',
  maxCount: 1,
}, {
  name: 'thumbnail',
  maxCount: 1,
}]));

my problem is that i am unable to set different folder for avatars and thumbnails. I can set only one folder for both :/ Anything else i tried ends with invalid CSRF. Thanks for anny suggestions.

EDIT: in one form i am using only one thing. So for example in profile update i have only possibility to change avatar in article adding i have only ability to change thumbnail of article. They not need to bey in upload fields can be seperate but dont know how.


Solution

  • I found on forum way how to go over csrf problem by adding ?_csrf={{csrfToken}} to the end of action in form so i can use official way of using multer :)