elementui?el-upload一次請求上傳多個文件的實(shí)現(xiàn)
在使用element中的el-upload是時,當(dāng)我們要上傳多個文件時,el-upload內(nèi)部會多次調(diào)用this.$refs.upload.submit();方法,從而實(shí)現(xiàn)多個文件上傳,但是有時候,我們希望,當(dāng)上傳多個文件的時候,只給后端發(fā)送一次請求,這樣就需要先把el-upload的自動上傳改為手動上傳:auto-upload=“false”
<el-upload
ref="upload"
:limit="10"
accept=".jpg,.gif,.png,.jpeg,.txt,.pdf,.doc,.docx,.xls,.xlsx"
name="files"
:multiple="true"
:action="baseURL"
:headers="myToken" <!-- 請求頭設(shè)置 -->
:on-change="handleFileChange"
:before-remove="handleFileRemove"
:auto-upload="false"
:file-list="upload.fileList"
>
<el-button slot="trigger" size="small" type="primary">選取文件</el-button>
</el-upload>
<el-button type="primary" @click="submitFileForm">確 定</el-button>data(){
return {
upload: {
fileList: [],
fileName: []
},
}
}通過FormData創(chuàng)建一個數(shù)據(jù)對象,并且將上傳的文件append到對象中
// 上傳發(fā)生變化鉤子
handleFileChange(file, fileList) {
this.upload.fileList = fileList;
},
// 刪除之前鉤子
handleFileRemove(file, fileList) {
this.upload.fileList = fileList;
},
// 提交上傳文件
submitFileForm() {
// 創(chuàng)建新的數(shù)據(jù)對象
let formData = new FormData();
// 將上傳的文件放到數(shù)據(jù)對象中
this.upload.fileList.forEach(file => {
formData.append('file', file.raw);
this.upload.fileName.push(file.name);
});
console.log("提交前",formData.getAll('file'));
// 文件名
formData.append('fileName', this.upload.fileName);
// 自定義上傳
this.$api.uploadFile(formData).then(response => {
console.log(response);
// if(response.code == 200){
// this.$refs.upload.clearFiles();
// this.msgSuccess('上傳成功!');
// }else{
// this.$message.error(response.msg);
// }
})
.catch(error => {
this.$message.error('上傳失敗!');
});
},使用自定義上傳的接口,而不是使用*this.$refs.upload.submit();*方法
注意上傳文件接口的請求頭中headers中的’Content-Type’要為’multipart/form-data’
// 封裝的上傳請求
uploadFile(params) {
return axios.post(`/shiro/swpe/permission/uploadOrStartProceBPS`, params,
{ headers: { 'Content-Type': 'multipart/form-data', token: window.localStorage.getItem('token')}})
},到此這篇關(guān)于elementui el-upload一次請求上傳多個文件的實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)elementui el-upload請求多文件上傳內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
解決element-ui的el-dialog組件中調(diào)用ref無效的問題
這篇文章主要介紹了解決element-ui的el-dialog組件中調(diào)用ref無效的問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-02-02
vue實(shí)現(xiàn)GitHub的第三方授權(quán)方法示例
本文主要介紹了vue實(shí)現(xiàn)GitHub的第三方授權(quán),文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-11-11
element ui 表格動態(tài)列顯示空白bug 修復(fù)方法
今天小編就為大家分享一篇element ui 表格動態(tài)列顯示空白bug 修復(fù)方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-09-09
手把手教你Vue3實(shí)現(xiàn)路由跳轉(zhuǎn)
Vue Router是Vue.js的官方路由器,它與Vue.js核心深度集成,使使用Vue.js構(gòu)建單頁應(yīng)用程序變得輕而易舉,下面這篇文章主要給大家介紹了關(guān)于Vue3實(shí)現(xiàn)路由跳轉(zhuǎn)的相關(guān)資料,需要的朋友可以參考下2022-08-08
詳解VUE-地區(qū)選擇器(V-Distpicker)組件使用心得
這篇文章主要介紹了詳解VUE-地區(qū)選擇器(V-Distpicker)組件使用心得,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-05-05

