【发布时间】:2018-12-27 02:46:46
【问题描述】:
我使用 multer 作为我的express app 的中间件,用于在服务器上上传文件,但它似乎无法正常工作,我使用邮递员或 curl 发送的每个帖子请求中都有一个空文件数组。
我在发送请求时已经将标头设置为 'Content-Type':'multipart/form-data'
app.js
行:12-43
const app = express();
const fileStorage = multer.diskStorage({
destination: (req, file, cb) => {
cb(null, 'images/');
},
Skipfilename: (req, file, cb) => {
cb(null, new Date().toISOString() + '-' + file.originalname);
}
});
const fileFilter = (req, file, cb) => {
if (
file.mimetype === 'audio/mp3' ||
file.mimetype === 'image/aug' ||
file.mimetype === 'image/jpg'
) {
cb(null, true);
}else {
cb(null, false);
}
};
app.use(bodyParser.urlencoded()); // x-www-form-urlencoded <form>
app.use(bodyParser.json()); // application/json
app.use(
multer({ storage: fileStorage, fileFilter: fileFilter }).any()
);
app.use('/images', express.static(path.join(__dirname, 'images')));
routes/admin.js
行:5-12
const router = express.Router();
router.post('/upload',adminController.upload);
module.exports = router;
controllers/admin.js
exports.upload = (req, res, next) => {
const files= req.files
console.log(req)
res.status(200).json({ files:files});
}
更新 邮递员请求截图
【问题讨论】:
-
你能把邮递员的请求贴在这里吗?
-
@ManjeetThakur 我刚刚上传了我的邮递员请求