gpt4 book ai didi

javascript - 穆尔特 : Setting different destinations in the destination Property

转载 作者:行者123 更新时间:2023-11-30 00:14:55 25 4
gpt4 key购买 nike

我有一个案例需要将图像存储在不同的目录中。所以我将 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 版本。我强烈建议您使用最新版本(出于安全原因)。

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

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

您将在文档中获得更多详细信息(旧版本链接)link

最新版本的 multer 工作方式略有不同。详细写如何使用新版本的 multer 将是一个很大的话题。您很容易在 stackoverflow 答案或文档的实际版本中找到 link

我只写了如何更改目标目录(文档中的示例):

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
})

关于javascript - 穆尔特 : Setting different destinations in the destination Property,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35099140/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com