我正在嘗試使用 multer 在快速端點中上傳影像檔案,但它不起作用。我不斷收到 500 意外欄位錯誤。我需要能夠從路線中讀取影像
const router = require('express').Router();
const multer = require('multer');
const path = require('path');
const storage = multer.diskStorage({
destination: './public/uploads/images',
filename: function (req, file, cb) {
cb(null, file.fieldname '-' Date.now()
path.extname(file.originalname));
}
});
const upload = multer({ storage: storage, limits: { fieldSize: 10 * 1024 * 1024 } });
/**
* @swagger
* /api/items/test:
* post:
* summary: add a new item
* tags: [Item]
* description: use to add a new item to a specific vendor
* requestBody:
* content:
* multipart/form-data:
* schema:
* type: object
* properties:
* fileName:
* type: string
* format: binary
*/
router.post('/test', upload.single('featuredImage'), async (req, res) => {
try {
console.log(req.file)
} catch (error) {
res.send(error.message);
}
});
uj5u.com熱心網友回復:
fileName應該對應于multer參考中給出的欄位名稱,即featuredImage,因此錯誤
嘗試替換它:
* properties:
* featuredImage:
* type: string
* format: binary
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/440651.html
標籤:javascript 节点.js 打字稿 穆尔特
下一篇:獲取條目的所有后代
