Vue+axios使用FormData方式向后端發(fā)送數(shù)據(jù)
前言
在前后端分離的項(xiàng)目中經(jīng)常使用到Vue+axios通過FormData的方式向后端(Java)發(fā)送表單數(shù)據(jù)(文字+圖片),但是在初次寫這個(gè)功能時(shí),可能會(huì)遇到很多問題,本文詳細(xì)的記錄了該功能的實(shí)現(xiàn)。
一、該功能應(yīng)用場景
前端向后端發(fā)送表單數(shù)據(jù)時(shí),可能會(huì)同時(shí)發(fā)送圖片和文本,這時(shí)會(huì)有一些需要注意的小細(xì)節(jié)。
二、代碼示例
1.前端(使用的ant-design-vue,element-ui同理)
代碼如下(示例):
<template>
<a-form-model :model="form" :label-col="labelCol" :wrapper-col="wrapperCol">
<a-form-model-item label="標(biāo)題">
<a-input v-model="form.title"/>
</a-form-model-item>
<a-form-model-item label="作者">
<a-input v-model="form.author"/>
</a-form-model-item>
<a-form-model-item label="類型">
<a-radio-group v-model="form.tag">
<a-radio :style="radioStyle" :value="1">
生活
</a-radio>
<a-radio :style="radioStyle" :value="2">
學(xué)習(xí)
</a-radio>
<a-radio :style="radioStyle" :value="3">
社會(huì)
</a-radio>
</a-radio-group>
</a-form-model-item>
<a-form-model-item label="圖片資源">
//此處開啟了多選,并重寫了brforeUpload方法,為了關(guān)閉自動(dòng)提交和將圖片轉(zhuǎn)換為base64編碼,同時(shí)聲明了文件列表的類型
<a-upload
:multiple="true"
:before-upload="beforeUpload"
name="file"
list-type="picture"
>
<a-button>
<a-icon type="upload"/>
上傳推文圖片
</a-button>
</a-upload>
</a-form-model-item>
<a-form-model-item label="正文">
<a-input v-model="form.content" type="textarea"/>
</a-form-model-item>
<a-form-model-item :wrapper-col="{ span: 14, offset: 4 }">
<a-button type="primary" @click="onSubmit">
提交
</a-button>
</a-form-model-item>
</a-form-model>
</template>
<script>
import axios from "axios";
export default {
data() {
return {
value: 1,
radioStyle: {
display: 'inline',
height: '30px',
lineHeight: '30px',
},
labelCol: {span: 4},
wrapperCol: {span: 14},
form: {
title: '',
author: '',
tag: '',
fileList: [],
content: ''
}
};
},
methods: {
beforeUpload(file, fileList) {
//將圖片轉(zhuǎn)換為Base64編碼
const reader = new FileReader()
reader.readAsDataURL(file)
reader.onload = () => {
const base64 = reader.result;
this.form.fileList.push(base64)
};
console.log(this.form.fileList)
//禁止直接提交圖片
return false;
},
//提交表單
onSubmit() {
let formData = new FormData
//此處需要和后端接收的參數(shù)名相同
formData.append("form", JSON.stringify(this.form))
axios.post(
//后端接口,自行修改
" http://localhost:8081/manage/submit",
formData,
{
headers: {'Content-Type': 'multipart/form-data'}
}
).then(res => {
console.log(res.data.message)
})
},
},
};
</script>該段代碼效果圖:

2.后端
代碼如下(示例):
@RestController
@Slf4j
@RequestMapping("/manage")
@CrossOrigin
public class ManageArticleController {
@Resource
ArticleService articleService;
@PostMapping("/submit")
public SystemResult<String> uploadImages(@RequestParam("form") String form)throws Exception {
//這兒將傳入的參數(shù)轉(zhuǎn)換為對(duì)應(yīng)的實(shí)體類
ArticleVo articleVo=JSON.parseObject(form,ArticleVo.class);
return SystemResult.success("上傳成功");
}
}實(shí)體類:
@Data
@Accessors(chain = true)
@AllArgsConstructor
@NoArgsConstructor
//對(duì)應(yīng)的實(shí)體類
public class ArticleVo implements Serializable {
private String title;
private String author;
private int tag;
private List<String> fileList;
private String content;
}三、效果演示
前端向后端發(fā)送數(shù)據(jù),后端打印顯示

在傳入到后端后,可以自行根據(jù)需求將數(shù)據(jù)存入數(shù)據(jù)庫
四、問題總結(jié)
1.前端發(fā)送的字段名和后端實(shí)體類的字段名不一致
將前端發(fā)送的數(shù)據(jù)名稱和后端修改一致
2.跨域問題
后端接口上添加注解@CrossOrigin
3.文件上傳的限制問題
在后端做相應(yīng)的文件上傳配置,比如上傳大小,或者前端也做相應(yīng)的驗(yàn)證。
4.不了解upload組件相關(guān)接口,導(dǎo)致功能無法實(shí)現(xiàn)
前往官網(wǎng)查看對(duì)應(yīng)組件的api,實(shí)在看不懂的話可以按照我的配置來寫。
到此這篇關(guān)于Vue+axios使用FormData方式向后端發(fā)送數(shù)據(jù)的文章就介紹到這了,更多相關(guān)Vue axios FormData后端發(fā)送內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Vue 路由 過渡動(dòng)效 數(shù)據(jù)獲取方法
這篇文章主要介紹了Vue 路由 過渡動(dòng)效 數(shù)據(jù)獲取方法,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2018-07-07
nuxt框架中路由鑒權(quán)之Koa和Session的用法
后臺(tái)管理頁面需要有登錄系統(tǒng),所以考慮做一下路由鑒權(quán),這篇文章主要介紹了nuxt框架中路由鑒權(quán)之Koa和Session的用法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-05-05
vue中使用[provide/inject]實(shí)現(xiàn)頁面reload的方法
這篇文章主要介紹了在vue中使用[provide/inject]實(shí)現(xiàn)頁面reload的方法,文中給大家提到了在vue中實(shí)現(xiàn)頁面刷新不同的方法,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-09-09
Vue.js學(xué)習(xí)筆記之 helloworld
vue是法語中視圖的意思,Vue.js是一個(gè)輕巧、高性能、可組件化的MVVM庫,同時(shí)擁有非常容易上手的API。有需要的小伙伴可以參考下2016-08-08

