浏览代码

文件下载功能

user5 2 年之前
父节点
当前提交
c5761a7a53
共有 1 个文件被更改,包括 26 次插入16 次删除
  1. 26 16
      src/views/modules/sys/project/AssessForm.vue

+ 26 - 16
src/views/modules/sys/project/AssessForm.vue

@@ -464,27 +464,37 @@
         }
       },
       async toHref (row) {
-        let suffix = row.name.substring(row.name.lastIndexOf('.') + 1)
-        if (suffix === 'pdf' || suffix === 'txt') {
-          if (row.id === null || row.id === undefined || row.id === '') {
-            this.ossService.downLoadAttach(row.raw.url)
-          } else {
-            this.ossService.downLoadAttach(row.url)
-          }
-          return
-        }
-        console.log('rr', row)
         const link = document.createElement('a')
-        link.setAttribute('download', row.name)
-        link.setAttribute('target', '_blank')
         if (row.id === null || row.id === undefined || row.id === '') {
           await this.ossService.getTemporaryUrl(row.raw.url).then((data) => {
-            link.setAttribute('href', data.data)
-            link.click()
+            const url = data.data // 完整的url则直接使用
+            // 这里是将url转成blob地址,
+            fetch(url).then(res => res.blob()).then(blob => { // 将链接地址字符内容转变成blob地址
+              link.href = URL.createObjectURL(blob)
+              console.log(link.href)
+              link.download = row.name || '' // 下载文件的名字
+              // a.download = url.split('/')[url.split('/').length -1] //  // 下载文件的名字
+              document.body.appendChild(link)
+              link.click()
+              // 在资源下载完成后 清除 占用的缓存资源
+              window.URL.revokeObjectURL(link.href)
+              document.body.removeChild(link)
+            })
           })
         } else {
-          link.setAttribute('href', row.temporaryUrl)
-          link.click()
+          const url = row.temporaryUrl // 完整的url则直接使用
+          // 这里是将url转成blob地址,
+          fetch(url).then(res => res.blob()).then(blob => { // 将链接地址字符内容转变成blob地址
+            link.href = URL.createObjectURL(blob)
+            console.log(link.href)
+            link.download = row.name || '' // 下载文件的名字
+            // a.download = url.split('/')[url.split('/').length -1] //  // 下载文件的名字
+            document.body.appendChild(link)
+            link.click()
+            // 在资源下载完成后 清除 占用的缓存资源
+            window.URL.revokeObjectURL(link.href)
+            document.body.removeChild(link)
+          })
         }
       },
       onPreview (url) {