簡體   English   中英

通過我驅動器中的圖像地址從文件或 postman 上傳照片

[英]upload photo from file or postman by image address in my drive

我有這樣的架構:

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()
  }
});

我想為每個朋友添加一張圖片,但我想通過在請求中在我的硬盤驅動器中添加圖片地址來上傳來自 postman 或播種文件的照片。 因為我有幾個朋友,所以我必須為每個朋友制作每張照片。 我想在 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"
   }
    ]
}

如何在后端創建此功能?

謝謝。

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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM