element-ui中el-upload多文件一次性上傳的實現
項目需求是多個文件上傳,在一次請求中完成,而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">上傳到服務器 </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: '', // 文件上傳數據(多文件合一)
fileList: [], // upload多文件數組
uploadData: {
fieldData: {
id: '', // 機構id,
}
},
}
}
methods:{
// 上傳文件
uploadFile(file) {
this.fileData.append('files', file.file); // append增加數據
},
// 上傳到服務器
submitUpload() {
let fieldData = this.uploadData.fieldData; // 緩存,注意,fieldData不要與fileData看混
if (fieldData.id === '') {
this.$message({
message: '請選擇上傳機構',
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(); // 提交調用uploadFile函數
this.fileData.append('pathId', fieldData.id); // 添加機構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 }?`);
},
// 選取文件超過數量提示
handleExceed(files, fileList) {
this.$message.warning(`當前限制選擇 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('當前文件已經存在!');
fileList.pop();
}
this.fileList = fileList;
},
}
此時就達到上傳4個文件只發(fā)送了一個xhr請求了

到此這篇關于element-ui中el-upload多文件一次性上傳的實現的文章就介紹到這了,更多相關el-upload多文件上傳內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
解決Vue + Echarts 使用markLine標線(precision精度問題)
這篇文章主要介紹了解決Vue + Echarts 使用markLine標線(precision精度問題),具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-07-07
詳解Vue中Computed與watch的用法與區(qū)別
這篇文章主要介紹了Vue中computed和watch的使用與區(qū)別,文中通過示例為大家進行了詳細講解,對Vue感興趣的同學,可以學習一下2022-04-04
vue-cli+iview項目打包上線之后圖標不顯示問題及解決方法
這篇文章主要介紹了解決vue-cli+iview項目打包上線之后圖標不顯示問題,本文通過兩種方法給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下2019-10-10

