简体   繁体   English

通过我驱动器中的图像地址从文件或 postman 上传照片

[英]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.

相关问题 使用javascript从任何人上传文件到我的Google驱动器 - Upload file to my google drive from anyone using javascript 图像文件上传与邮递员工作,但我的代码不工作。 是否需要做任何改变? - Image file upload with postman working, but my code is not working. is there any changes need to be done? 如何将图像从 url 上传到 Google Drive? - How to upload an image from an url to Google Drive? 如何使用fabricjs从HTMlL5 canvas中的本地硬盘驱动器上传图像文件 - How to upload image file from local hard drive in HTMlL5 canvas using fabricjs AngularJS照片/文件上传 - AngularJS photo/file upload 从浏览器而不是邮递员提交时,Django端点上载图像失败 - Django endpoint to upload image fails when submitted from browser but not postman Photo Sphere全景照片上传验证 - Photo sphere image upload validation 谷歌云端硬盘上传文件 - Google Drive Upload File 从移动设备/邮递员上传回送组件存储文件 - Loopback component storage file upload from mobile devices/postman 文件上传适用于 Postman 但不适用于从浏览器运行的 Javascript - File Upload is working with Postman but not with Javascript running from browser
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM