element-ui中el-upload多文件一次性上傳的實現(xiàn)
項目需求是多個文件上傳,在一次請求中完成,而ElementUI的上傳組件是每個文件發(fā)一次上傳請求,因此我們借助FormData的格式向后臺傳文件組
html代碼
<div class="upload-file"> <el-upload accept=".xlsx" ref="upload" multiple :limit="5" action="http://xxx.xxx.xxx/personality/uploadExcel" :on-preview="handlePreview" :on-change="handleChange" :on-remove="handleRemove" :on-exceed="handleExceed" :file-list="fileList" :http-request="uploadFile" :auto-upload="false"> <el-button slot="trigger" size="small" type="primary">選取文件</el-button> <el-button style="margin-left: 133px;" size="small" type="success" @click="submitUpload">上傳到服務(wù)器 </el-button> <div slot="tip" class="el-upload__tip">只能上傳xlsx文件,且不超過100m</div> </el-upload> </div>
修改:auto-upload="false"屬性為false,阻止組件的自動上傳
:http-request="uploadFile"覆蓋上傳事件
@click=“submitUpload”,給上傳按鈕綁定事件

data() {
return {
fileData: '', // 文件上傳數(shù)據(jù)(多文件合一)
fileList: [], // upload多文件數(shù)組
uploadData: {
fieldData: {
id: '', // 機構(gòu)id,
}
},
}
}
methods:{
// 上傳文件
uploadFile(file) {
this.fileData.append('files', file.file); // append增加數(shù)據(jù)
},
// 上傳到服務(wù)器
submitUpload() {
let fieldData = this.uploadData.fieldData; // 緩存,注意,fieldData不要與fileData看混
if (fieldData.id === '') {
this.$message({
message: '請選擇上傳機構(gòu)',
type: 'warning'
})
} else if (this.fileList.length === 0) {
this.$message({
message: '請先選擇文件',
type: 'warning'
})
} else {
const isLt100M = this.fileList.every(file => file.size / 1024 / 1024 < 100);
if (!isLt100M) {
this.$message.error('請檢查,上傳文件大小不能超過100MB!');
} else {
this.fileData = new FormData(); // new formData對象
this.$refs.upload.submit(); // 提交調(diào)用uploadFile函數(shù)
this.fileData.append('pathId', fieldData.id); // 添加機構(gòu)id
this.fileData.append('loginToken', this.loginToken); // 添加token
post(this.baseUrlData.url_02 + ":8090/personality/uploadExcel", this.fileData).then((response) => {
if (response.data.code === 0) {
this.$message({
message: "上傳成功",
type: 'success'
});
this.fileList = [];
} else {
this.$message({
message: response.data.desc,
type: 'error'
})
}
});
}
}
},
//移除
handleRemove(file, fileList) {
this.fileList = fileList;
// return this.$confirm(`確定移除 ${ file.name }?`);
},
// 選取文件超過數(shù)量提示
handleExceed(files, fileList) {
this.$message.warning(`當(dāng)前限制選擇 5 個文件,本次選擇了 ${files.length} 個文件,共選擇了 ${files.length + fileList.length} 個文件`);
},
//監(jiān)控上傳文件列表
handleChange(file, fileList) {
let existFile = fileList.slice(0, fileList.length - 1).find(f => f.name === file.name);
if (existFile) {
this.$message.error('當(dāng)前文件已經(jīng)存在!');
fileList.pop();
}
this.fileList = fileList;
},
}
此時就達(dá)到上傳4個文件只發(fā)送了一個xhr請求了

到此這篇關(guān)于element-ui中el-upload多文件一次性上傳的實現(xiàn)的文章就介紹到這了,更多相關(guān)el-upload多文件上傳內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Vue實現(xiàn)側(cè)邊菜單欄手風(fēng)琴效果實例代碼
本文通過一段簡單的代碼給大家介紹了基于純vue實現(xiàn)側(cè)邊菜單欄手風(fēng)琴效果,代碼很簡單,感興趣的朋友跟隨腳本之家小編一起看看吧2018-05-05
關(guān)于json-bigint處理大數(shù)字問題
這篇文章主要介紹了關(guān)于json-bigint處理大數(shù)字問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-05-05
解決Vue + Echarts 使用markLine標(biāo)線(precision精度問題)
這篇文章主要介紹了解決Vue + Echarts 使用markLine標(biāo)線(precision精度問題),具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-07-07
詳解Vue中Computed與watch的用法與區(qū)別
vue-cli+iview項目打包上線之后圖標(biāo)不顯示問題及解決方法

