Multer:在目的地属性中设置不同的目的地

我有一个情况,我需要将图像存储在不同的目录中。 所以我把这个设置成了。

app.use(multer({ dest: path.join(__dirname, '`public/assets/img/profile`'), rename: function (fieldname, filename, req, res) { if(req.session.user) return req.session.user.id; else if(req.session.doctor) return req.session.doctor.id; } })); 

不过,我需要更多的目的地来存储图像。

 public/assets/img/picture1 

我曾经见过类似的问题,但我不明白其中的任何一个。 任何帮助将是伟大的。

你的例子来自相当老的版本的multer。 我强烈build议您使用最新版本(由于安全原因)。

如果您确定您需要旧版本,那么只需添加到您的MULTER选项:

 app.use(multer({ //... changeDest: function(dest, req, res) { return dest + '/user1'; } //... })); 

更多的细节你会得到文档(链接到旧版本) 链接

最新版本的MULTER的作品有一点不同。 这将是大的offtopic详细写如何使用新版本的multer。 你可以很容易地find在stackoverflow答案或从实际版本的文件链接

我只写如何更改目标目录(例如从文档):

 var storage = multer.diskStorage({ destination: function (req, file, cb) { cb(null, '/tmp/my-uploads') }, filename: function (req, file, cb) { cb(null, file.fieldname + '-' + Date.now()) } }) var upload = multer({ storage: storage }) app.post('/profile', upload.single('picture'), function (req, res, next) { // req.file is the `picture` file // req.body will hold the text fields, if there were any })