Commit 803bd5e3c40b5132060acb67cb189afbf00abda2
1 parent
1a60d0ce
v3.8.4:修复多文件上传报错出现的异常问题
Showing
1 changed file
with
36 additions
and
22 deletions
yudao-ui-admin/src/components/FileUpload/index.vue
| 1 | 1 | <template> |
| 2 | 2 | <div class="upload-file"> |
| 3 | 3 | <el-upload |
| 4 | - multiple | |
| 5 | - :action="uploadFileUrl" | |
| 6 | - :before-upload="handleBeforeUpload" | |
| 7 | - :file-list="fileList" | |
| 8 | - :limit="limit" | |
| 9 | - :on-error="handleUploadError" | |
| 10 | - :on-exceed="handleExceed" | |
| 11 | - :on-success="handleUploadSuccess" | |
| 12 | - :show-file-list="false" | |
| 13 | - :headers="headers" | |
| 14 | - class="upload-file-uploader" | |
| 15 | - ref="upload" | |
| 4 | + multiple | |
| 5 | + :action="uploadFileUrl" | |
| 6 | + :before-upload="handleBeforeUpload" | |
| 7 | + :file-list="fileList" | |
| 8 | + :limit="limit" | |
| 9 | + :on-error="handleUploadError" | |
| 10 | + :on-exceed="handleExceed" | |
| 11 | + :on-success="handleUploadSuccess" | |
| 12 | + :show-file-list="false" | |
| 13 | + :headers="headers" | |
| 14 | + class="upload-file-uploader" | |
| 15 | + ref="fileUpload" | |
| 16 | 16 | > |
| 17 | 17 | <!-- 上传按钮 --> |
| 18 | 18 | <el-button size="mini" type="primary">选取文件</el-button> |
| ... | ... | @@ -72,6 +72,7 @@ export default { |
| 72 | 72 | return { |
| 73 | 73 | number: 0, |
| 74 | 74 | uploadList: [], |
| 75 | + baseUrl: process.env.VUE_APP_BASE_API, | |
| 75 | 76 | uploadFileUrl: process.env.VUE_APP_BASE_API + "/admin-api/infra/file/upload", // 请求地址 |
| 76 | 77 | headers: { Authorization: "Bearer " + getAccessToken() }, // 设置上传的请求头部 |
| 77 | 78 | fileList: [], |
| ... | ... | @@ -118,7 +119,8 @@ export default { |
| 118 | 119 | } |
| 119 | 120 | const isTypeOk = this.fileType.some((type) => { |
| 120 | 121 | if (file.type.indexOf(type) > -1) return true; |
| 121 | - return !!(fileExtension && fileExtension.indexOf(type) > -1); | |
| 122 | + if (fileExtension && fileExtension.indexOf(type) > -1) return true; | |
| 123 | + return false; | |
| 122 | 124 | }); |
| 123 | 125 | if (!isTypeOk) { |
| 124 | 126 | this.$modal.msgError(`文件格式不正确, 请上传${this.fileType.join("/")}格式文件!`); |
| ... | ... | @@ -147,15 +149,17 @@ export default { |
| 147 | 149 | this.$modal.closeLoading() |
| 148 | 150 | }, |
| 149 | 151 | // 上传成功回调 |
| 150 | - handleUploadSuccess(res) { | |
| 151 | - // edit by 芋道源码 | |
| 152 | - this.uploadList.push({ name: res.data, url: res.data }); | |
| 153 | - if (this.uploadList.length === this.number) { | |
| 154 | - this.fileList = this.fileList.concat(this.uploadList); | |
| 155 | - this.uploadList = []; | |
| 156 | - this.number = 0; | |
| 157 | - this.$emit("input", this.listToString(this.fileList)); | |
| 152 | + handleUploadSuccess(res, file) { | |
| 153 | + if (res.code === 200) { | |
| 154 | + // edit by 芋道源码 | |
| 155 | + this.uploadList.push({ name: res.data, url: res.data }); | |
| 156 | + this.uploadedSuccessfully(); | |
| 157 | + } else { | |
| 158 | + this.number--; | |
| 158 | 159 | this.$modal.closeLoading(); |
| 160 | + this.$modal.msgError(res.msg); | |
| 161 | + this.$refs.fileUpload.handleRemove(file); | |
| 162 | + this.uploadedSuccessfully(); | |
| 159 | 163 | } |
| 160 | 164 | }, |
| 161 | 165 | // 删除文件 |
| ... | ... | @@ -163,6 +167,16 @@ export default { |
| 163 | 167 | this.fileList.splice(index, 1); |
| 164 | 168 | this.$emit("input", this.listToString(this.fileList)); |
| 165 | 169 | }, |
| 170 | + // 上传结束处理 | |
| 171 | + uploadedSuccessfully() { | |
| 172 | + if (this.number > 0 && this.uploadList.length === this.number) { | |
| 173 | + this.fileList = this.fileList.concat(this.uploadList); | |
| 174 | + this.uploadList = []; | |
| 175 | + this.number = 0; | |
| 176 | + this.$emit("input", this.listToString(this.fileList)); | |
| 177 | + this.$modal.closeLoading(); | |
| 178 | + } | |
| 179 | + }, | |
| 166 | 180 | // 获取文件名称 |
| 167 | 181 | getFileName(name) { |
| 168 | 182 | if (name.lastIndexOf("/") > -1) { |
| ... | ... | @@ -178,7 +192,7 @@ export default { |
| 178 | 192 | for (let i in list) { |
| 179 | 193 | strs += list[i].url + separator; |
| 180 | 194 | } |
| 181 | - return strs !== '' ? strs.substr(0, strs.length - 1) : ''; | |
| 195 | + return strs != '' ? strs.substr(0, strs.length - 1) : ''; | |
| 182 | 196 | } |
| 183 | 197 | } |
| 184 | 198 | }; | ... | ... |