问题
axios,formData提交的时候,怎么一起提交文件和数据
前台vue
submitForm(event) {
event.preventDefault();
let formData = new FormData();
formData.append("file", this.file);
formData.append("data", {
"aa":"aa",
"bb":"bb",
"cc":"cc"
});
let config = {
headers: {
"Content-Type": "multipart/form-data"
}
};
axios
.post("http://localhost:3000/upload", formData, config)
.then(function(res) {
if (res.status === 2000) {
/*这里做处理*/
}
});
},
后台node
router.post('/upload', upload.single('file'), async(ctx, next) => {
// console.log(ctx.req)
console.log(ctx.req.body.data.aa)
console.log(ctx.req.file)
await userModel.imgInsertInto([ctx.req.file.filename, new Date()])
.then(result => {
ctx.body = {
code: 200,
msg: '图片上传成功',
data: result
}
})
.catch(error => {
console.log(error)
ctx.body = false;
})
// ctx.body = {
// filename: ctx.req.file.filename //返回文件名
// }
})
打印接收到的数据
眼眸繁星
HUWWW
相关分类