koa 表单问题
发布于 3 年前 作者 linima 5829 次浏览 来自 问答

###相关的HTML文件

<form method="post" action="add" enctype='multipart/form-data'>
<input type="text" name="title">
<input type="file" name="thumbnail" accept="image/*">
<button type="submit">Submit</button>
</form>

###blog.js

const router = require('koa-router')();
const controller = require('blogController');
const multer = require('koa-multer');
const upload = multer({ dest: './public/uploads/' });
router.post('/add', upload.single('thumbnail'),  controller.blogAdd);
module.exports = router;

###blogController.js

const mongoose = require('mongoose');
const Blog = mongoose.model('Blog');
exports.blogAdd =  async (ctx, next) => {
	let title = ctx.request.body.title;
	let thumbnail = ctx.request.body.thumbnail;
	ctx.body = await ctx.request.body;
	//const blog = new Blog({
	//	title: title,
	//	thumbnail:  thumbnail
	//})
	//blog.save();
}

如上,表单中有多个类型的enctype=‘multipart/form-data’, 用koa-multer配置文件上传后,文件可以提交成功,但ctx.request.body为空{}, 怎么保存提交的表单数据,上面代码应该怎么修改?

3 回复

跳转一下中间件顺序即可

请问一下哥们怎么解决的?

@i5ting 狼叔可以具体说下什么意思嘛?怎么跳转?

回到顶部