如何使用 multer 从同一 mongodb 集合的多个字段上传文件
How to upload files from multiple fields of same mongodb collection using multer
我想上传来自同一集合的多个领域的图像。我可以使用一个字段上传。
路线:service.js
router.post('/register', upload.array('daycare.DCImage',10),(req, res) => {
var paths = req.files.map(file => file.path)
const newService = new Services(req.body);
newService.daycare.DCImage = paths
newService
.save(newService)
.then(data => {
res.send(data);
})
.catch(err => {
res.status(500).send({
message:
err.message || "Some error occurred while creating the Service."
});
});
})
喜欢daycare.DCImage我想制作grooming.groomingImage,trainer.TrainingImage, dogwalker.DWImage, breeding.breedingImages 字段也方便图片上传.
此代码目前运行良好,我如何更改代码以获得所需的功能?
我试过包括额外的 upload.array() 比如
router.post('/register', upload.array('daycare.DCImage',10), upload.array('grooming.GroomingImage',10 ), upload.array('dogwalker.DWImage', 10) , upload.array('trainer.TrainingImage', 10 ), upload.array('breeding.breedingImage', 10) ,(req, res) )
以及同一 upload.array()
中的其他字段
router.post('/register', upload.array('daycare.DCImage', 'grooming.GroomingImage', 'trainer.TrainingImage' , 'breeding.breedingImage', 10),(req, res) )
但是没有用。
这是模型service.js
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const User = require('./User');
const ServiceSchema = new Schema({
daycare : {
description : {type : String},
DCImage : [{type : String}],
additionalServices : {type : String},
},
grooming : {
description : {type : String},
GroomingImage : [{type : String}],
additionalServices : {type : String},
},
dogwalker : {
description : {type : String},
DWImage : [{type : String}],
additionalServices : {type : String},
},
trainer : {
description : {type : String},
TrainingImage : [{type : String}],
price : {
daily : {type : String},
weekly : {type : String},
monthly : {type : String}
},
additionalServices : {type : String},
},
breeding : {
breed : {type : String},
age : {type : String},
breedingImages:[{type:String}],
},
});
module.exports = Services = mongoose.model('Services', ServiceSchema);
使用多重上传。字段像这样,
router.post('/register', upload.fields([
{ name: "field_name", maxCount: 1 },
{ name: "field_name", maxCount: 1 },
{ name: "field_name", maxCount: 1 },
{ name: "field_name", maxCount: 1 }
])
,(req, res) );
然后像这样上传功能,
const upload = multer({
storage: 'path-to-storage',
limits: { fileSize: 100000 },
key: function (req, file, cb) {
cb(null, file.originalname);
},
});
我想上传来自同一集合的多个领域的图像。我可以使用一个字段上传。
路线:service.js
router.post('/register', upload.array('daycare.DCImage',10),(req, res) => {
var paths = req.files.map(file => file.path)
const newService = new Services(req.body);
newService.daycare.DCImage = paths
newService
.save(newService)
.then(data => {
res.send(data);
})
.catch(err => {
res.status(500).send({
message:
err.message || "Some error occurred while creating the Service."
});
});
})
喜欢daycare.DCImage我想制作grooming.groomingImage,trainer.TrainingImage, dogwalker.DWImage, breeding.breedingImages 字段也方便图片上传.
此代码目前运行良好,我如何更改代码以获得所需的功能?
我试过包括额外的 upload.array() 比如
router.post('/register', upload.array('daycare.DCImage',10), upload.array('grooming.GroomingImage',10 ), upload.array('dogwalker.DWImage', 10) , upload.array('trainer.TrainingImage', 10 ), upload.array('breeding.breedingImage', 10) ,(req, res) )
以及同一 upload.array()
中的其他字段router.post('/register', upload.array('daycare.DCImage', 'grooming.GroomingImage', 'trainer.TrainingImage' , 'breeding.breedingImage', 10),(req, res) )
但是没有用。
这是模型service.js
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const User = require('./User');
const ServiceSchema = new Schema({
daycare : {
description : {type : String},
DCImage : [{type : String}],
additionalServices : {type : String},
},
grooming : {
description : {type : String},
GroomingImage : [{type : String}],
additionalServices : {type : String},
},
dogwalker : {
description : {type : String},
DWImage : [{type : String}],
additionalServices : {type : String},
},
trainer : {
description : {type : String},
TrainingImage : [{type : String}],
price : {
daily : {type : String},
weekly : {type : String},
monthly : {type : String}
},
additionalServices : {type : String},
},
breeding : {
breed : {type : String},
age : {type : String},
breedingImages:[{type:String}],
},
});
module.exports = Services = mongoose.model('Services', ServiceSchema);
使用多重上传。字段像这样,
router.post('/register', upload.fields([
{ name: "field_name", maxCount: 1 },
{ name: "field_name", maxCount: 1 },
{ name: "field_name", maxCount: 1 },
{ name: "field_name", maxCount: 1 }
])
,(req, res) );
然后像这样上传功能,
const upload = multer({
storage: 'path-to-storage',
limits: { fileSize: 100000 },
key: function (req, file, cb) {
cb(null, file.originalname);
},
});