嘿,我希望图像在通过所有验证逻辑后都应该上传,然后应该将图像上传到服务器,但是我在这里使用multer中间件,并且使用multer中间件,我如何实现它,以便我完成所有验证后,将图像上传到云服务器。
const cloudinary = require("cloudinary");
const cloudinaryStorage = require("multer-storage-cloudinary");
cloudinary.config({
cloud_name: "*********",
api_key: "*************",
api_secret: "*************"
});
exports.cloudinaryStorage = cloudinaryStorage({
cloudinary,
folder: "game_zone",
allowedFormats: ["jpg", "png", "jpeg", "gid", "pdf"],
filename: function(req, file, cb) {
cb(null, Date.now() + file.originalname); // The file on cloudinary would have the same name as the original file name
}
});
const gameSchema = new mongoose.Schema({
name: {
type: String,
required: true
},
img: [
{
type: String,
default: "default"
}
],
categories: [
{
type: String
}
]
});
router.post("/", upload.array("gameImage", 5), async (req, res) => {
const { error } = validate(req.body);
if (error) return res.status(400).send(error.details[0].message);
game = new Game(gameData);
const result = await game.save();
res.json({
result: result
});
});
现在可以正常工作,但不能以我想要的方式使用。 您能建议我在这里更改实施方式吗?
答案 0 :(得分:0)
您可能要尝试使用cloudinary上传小部件。它可以帮助您进行验证(检查格式),并在获得响应时将其保存在数据库中。 你可以在这里读更多关于它的内容: https://cloudinary.com/documentation/embed_widgets_players#upload_widget