[英]upload photo from file or postman by image address in my drive
I have a schema like this:我有这样的架构:
const Person = new mongoose.Schema({
username: {
type: String,
required: [true, "Name is a required field"],
unique: [true, "Another Person with the same name already exists"],
trim: true
},
friends: [
{
name: {
type: String,
required: [true, "Every friend must have a name"],
unique: [true, "Another friend with the same name already exists"],
trim: true
},
photo: String,
favoriteFood: String
}
],
createdAt: {
type: Date,
default: Date.now()
}
});
I want to add an image for each friend, but I want to upload the photo from something like postman or a seeder file by adding image address in my hard drive in the request.我想为每个朋友添加一张图片,但我想通过在请求中在我的硬盘驱动器中添加图片地址来上传来自 postman 或播种文件的照片。 because I have several friends, I have to make each photo specific for each friend.
因为我有几个朋友,所以我必须为每个朋友制作每张照片。 I want to do something like this in postman body or in a file as a seeder to fill database or add data to it.
我想在 postman 正文中或在文件中作为播种器来填充数据库或向其中添加数据。
{
"username": "aUsername",
"friends": [
{
"name": "friend1",
"photo": "./home/user/photos/photo1.jpg",
"favoriteFood": "Pizza"
},
{
"name": "friend2",
"photo": "./home/user/photos/photo2.jpg",
"favoriteFood": "Sushi"
},
{
"name": "friend3",
"photo": "./home/user/photos/photo3.jpg",
"favoriteFood": "Hamburger"
}
]
}
how can I create this feature in the backend?如何在后端创建此功能?
thanks.谢谢。
const multer = require('multer');
const storage = multer.diskStorage({
destination: function(req, file, callback) {
callback(null, '/src/my-images');
},
filename: function (req, file, callback) {
callback(null, file.fieldname);
}
});
app.post('/', upload.single('file'), (req, res) => {
if (!req.file) {
console.log("No file received");
return res.send({
success: false
});
} else {
console.log('file received');
return res.send({
success: true
})
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.