1、template:
<el-upload class="upload-file-demo" action="上傳的url" ref="uploadComp" :auto-upload="false" //禁止自動上傳 :show-file-list='true' //顯示上傳文件名 :file-list="certificates2" //回顯文件 :before-upload="beforeUpload" //文件上傳前 :on-change="handlePictureCardPreview2" //文件改變的時候 :on-success="handleAvatarSuccess2" //文件上傳成功回調 :on-exceed="handleExceed2" //文件限制上傳 :on-remove="handleRemove2" //文件刪除 :limit="1" //限制個數 :disabled="loading" :data="uploadData" //上傳攜帶的參數 accept=".xls,.xlsx,.png,.jpe,.jpeg" > <el-button :loading="loading" class="upload_btn" size="small" type="primary">上傳明細</el-button> </el-upload>
2、script:
// 文件上傳 - 上傳前 beforeUpload(file, fileList) { this.uploadData = { tradeOrder: this.dataList.id }; //上傳攜帶的參數名 let promise = new Promise((resolve) => { this.$nextTick(function () { resolve(true); }); }); return promise; }, // 文件上傳 - 文件選中后上傳 handlePictureCardPreview2(file, fileList) { this.$refs.uploadComp.submit(); }, //文件上傳 - 成功回調 handleAvatarSuccess2(res, file, fileList) { // console.log(res); }, // 文件上傳 - 限制上傳 handleExceed2(files, fileList) { this.$message.warning(`當前限制選擇 1 個文件,本次選擇了 ${files.length} 個文件,共選擇了 ${files.length + fileList.length} 個文件`); }, // 文件上傳 - 刪除 handleRemove2(file, fileList) { var that = this; fileList.forEach((item, index) => { that.certificate2.push(item.url); }) },