Explorar o código

普通文件上传优化

huangguoce hai 3 semanas
pai
achega
feb3e0b31c
Modificáronse 1 ficheiros con 55 adicións e 60 borrados
  1. 55 60
      src/views/common/UpLoadComponent.vue

+ 55 - 60
src/views/common/UpLoadComponent.vue

@@ -221,12 +221,13 @@ export default {
               this.fileLoading = true
             }
           })
-        } else {
-          this.dataList.push(item)
-          this.dataListNew.push(item)
-          this.fileList.push(item)
-          this.fileLoading = true
         }
+        // else {
+        //   this.dataList.push(item)
+        //   this.dataListNew.push(item)
+        //  this.fileList.push(item)
+        //  this.fileLoading = true
+        // }
       }
       // this.dataList = JSON.parse(JSON.stringify(fileList))
       // this.dataListNew = JSON.parse(JSON.stringify(fileList))
@@ -333,66 +334,61 @@ export default {
       }
     },
     async changes(file, fileList) {
-      // if (file.status !== 'ready') {
-      //   return
-      // }
-
-
-      if (file.status !== 'ready') {
-        let fileListNamesList = []
-        console.log(JSON.stringify(this.fileList));
-        this.fileList.forEach(fileItem => {
-          fileListNamesList.push(fileItem.name)
-        })
+      if (file.status == 'ready') return //阻止上传多次触发changes
 
-        if (fileListNamesList.indexOf(file.name) != -1) {
-          console.log(fileListNamesList);
+      let fileListNamesList = []
+      this.fileList.forEach(fileItem => {
+        fileListNamesList.push(fileItem.name)
+      })
 
-          this.$message.error(`${file.name}已存在,无法重复上传`)
+      if (fileListNamesList.indexOf(file.name) != -1) {
+        this.$message.error(`${file.name}已存在,无法重复上传`)
+        return
+      } else {
+        if (!beforeAvatarUpload(file, fileList, this.maxValue)) {
+          this.$message.error('文件大小不能超过 ' + this.maxValue + ' MB!')
           return
-        } else {
+        }
 
-          if (!beforeAvatarUpload(file, fileList, this.maxValue)) {
-            this.$message.error('文件大小不能超过 ' + this.maxValue + ' MB!')
+        let item1 = JSON.parse(JSON.stringify(file))
+        // if (fileListNames.length === 0 || fileListNames.indexOf(item1.name) === -1) {
+        const fileUrl = item1.raw ? item1.raw.url : item1.url;
+        // 检查临时 URL 是否有效
+        if (fileUrl && fileUrl.trim() !== '') {
+          try {
+            const temporaryUrl = await this.ossService.getTemporaryUrl(fileUrl);
+            if (!temporaryUrl || temporaryUrl.trim() === '') {
+              // 如果临时 URL 无效,则标记该文件为无效并删除
+              this.$message.warning({ message: `文件:${file.name}获取失败,上传失败。` });
+              return
+            } else {
+              // 如果临时 URL 有效,继续添加文件到有效文件列表
+              file.lsUrl = temporaryUrl;
+            }
+          } catch (error) {
+            // 如果临时 URL 获取失败,也标记该文件为无效
+            this.$message.warning({ message: `文件:${file.name}获取失败,上传失败。` });
             return
           }
-          // this.dataListNew = []
-          // this.dataList.forEach((item) => {
-          //   this.dataListNew.push(item)
-          // })
-          var fileListNames = []
-          for (let fileItem of this.fileList) {
-            fileListNames.push(fileItem.name)
-          }
-          // for (let item of fileList) {
-
+        }
 
-          let item1 = JSON.parse(JSON.stringify(file))
-          // if (fileListNames.length === 0 || fileListNames.indexOf(item1.name) === -1) {
-          this.fileList.push(item1)
-          // }
-          item1.createTime = moment(new Date()).format('YYYY-MM-DD HH:mm:ss')
-          item1.createBy = {
-            id: '',
-            name: ''
-          }
-          item1.createBy.id = this.$store.state.user.id
-          item1.createBy.name = this.$store.state.user.name
-          this.dataListNew.push(item1)
-          this.dataList.push(item1)
-          // }
-          for (let item of this.dataListNew) {
-            if (item.raw !== undefined && item.raw !== null && item.raw !== {}) {
-              item.url = item.raw.url
-              if (item.raw.url !== undefined && item.raw.url !== null && item.raw.url !== {}) {
-                this.ossService.getTemporaryUrl(item.raw.url).then((data) => {
-                  item.lsUrl = data
-                })
-              }
-            }
+        // 添加url
+        if (item1.raw && item1.raw.url) {
+          item1.url = item1.raw.url
+        }
 
-          }
+        this.fileList.push(item1)
+        // }
+        item1.createTime = moment(new Date()).format('YYYY-MM-DD HH:mm:ss')
+        item1.createBy = {
+          id: '',
+          name: ''
         }
+        item1.createBy.id = this.$store.state.user.id
+        item1.createBy.name = this.$store.state.user.name
+
+        this.dataListNew.push(item1)
+        this.dataList.push(item1)
       }
 
     },
@@ -412,10 +408,9 @@ export default {
     toHref(row) {
       toHref(row)
     },
-    async deleteById(row, index, fileList) {
-      // this.$refs.upload.handleRemove(this.dataListNew[index])
-      await this.dataListNew.splice(index, 1)
-      await this.dataList.splice(index, 1)
+    deleteById(row, index, fileList) {
+      this.dataListNew.splice(index, 1)
+      this.dataList.splice(index, 1)
       if (this.commonJS.isNotEmpty(row.id)) {
         this.ossService.deleteMsgById(row.id)
       }