[英]How can I upload files to S3 using multer without having multer store a copy of the files locally?
[英]How I can upload pictures using multer for array of objects
我正在使用 nodeJS、express 和 mongoose。 我有一个表格模式,它也可以上传多张图片。 要上传我正在使用 multer 的图片。 在 multer 中,我正在使用 multer.fields() 上传带有特定自己的密钥名称的图片,以上传像 father_pic、mother_pic 和 brother_pic 这样的图片。 现在的问题是,可以有多个兄弟,所以我如何为与他的姓名和职业相关的兄弟图片上传多张图片?
模型架构
const familyDetailsSchema = new mongoose.Schema(
{
language:{
type:String,
require:true
},
father:{
fatherName:{
type:String,
require:true
},
fatherProfession:{
type:String,
require:true
},
fatherImage:{
type:String,
require:true
}
},
mother:{
motherName:{
type:String,
require:true
},
motherProfession:{
type:String,
require:true
},
motherImage:{
type:String,
require:true
}
},
brothers:[
{
name:String,
profession:String,
image:String,
}
],
details:{
type:String,
require:true
},
user_id: {
type: mongoose.Schema.Types.ObjectId,
ref: 'User'
},
createdAt: {
type: Number
},
updatedAt: {
type: Number,
}
},
{
timestamps:true,
}
)
这是路由代码
router.post('/familyDetails',
[
upload.fields([
{
name: 'father_pic',
},
{
name: 'mother_pic',
},
{
name:'brother_pic'
}
]),
],
async (req, res) => {}
要将此输入视为数组,您可以使用 multer.array,请遵循以下简单示例:
router.post('/family',
[
upload.array('family_pictures'),
],
async (req, res) => {
const images = req.files['family_pictures'];
images.forEach((image) => {
});
}
);
要了解更多详细信息,您可以在 npm 使用 multer 文档: https ://www.npmjs.com/package/multer
您是否考虑过使用 Multer 的.any()
方法,这将使您能够上传任何名称的文件。 然后,您不必担心文件的数量和文件名。
router.post('/familyDetails', upload.any(), async (req, res) => {
console.log('Files: ', req.files);
})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.