我使用shortid为每个上传的唯一ID,并为post处理程序进行multer。
我正在上传一些输入以及图像。我想将每个上传内容存储在“upload / XXXXX”中我在app.post(...)上生成唯一ID,并想知道如何将此id发送到multer。
如果同时完成2个帖子,使用全局变量会有问题吗?
var Storage = multer.diskStorage({
destination: function (req, file, callback) {
callback(null, path.join(__dirname, 'uploads', XXXXX)); //Unique id for file is the same as Folder
},
filename: function (req, file, callback) {
callback(null, XXXXX); //Unique id
}
});
var upload = multer({
storage: Storage
}).single('pic');
//tell express what to do when the route is requested
app.post('/fbshare', function (req, res, next) {
let ui = shortid();
upload(req, res, function (err) {
if (err) {
return res.end("Something went wrong!");
}
return res.end("File uploaded sucessfully!.");
});
});
我怎么能从app.post()通过ui到存储?或者,如果你有更好的解决方案,我会全力以赴。
谢谢
结局Solutyon
var Storage = multer.diskStorage({
destination: function (req, file, callback) {
fs.mkdir(path.join(__dirname, 'uploads', req.ui), function(){
callback(null, path.join(__dirname, 'uploads', req.ui));
});
},
filename: function (req, file, callback) {
callback(null, req.ui + file.originalname.substring(file.originalname.indexOf('.'), file.originalname.length));
}
});
var upload = multer({
storage: Storage
}).single('pic');
//tell express what to do when the route is requested
app.post('/fbshare', function (req, res, next) {
req.ui = shortid();
upload(req, res, function (err) {
if (err) {
return res.end("Something went wrong!");
}
return res.end("File uploaded sucessfully!.");
});
});
这个怎么样?
app.post('/fbshare', function (req, res, next) {
req.ui = shortid(); // create the id in the request
....
然后在闷车
var Storage = multer.diskStorage({
destination: function (req, file, callback) {
callback(null, path.join(__dirname, 'uploads', req.ui)); //Unique id for
....
和filename
一样