ソースを参照

feat: 附件下载支持指定文件名

cuiyalong 7 ヶ月 前
コミット
12c4b198e7

+ 17 - 2
apps/bigmember_pc/src/composables/attachment-download/component/AttachmentDownload.vue

@@ -58,7 +58,10 @@
 <script>
 import { mapState, mapGetters } from 'vuex'
 import { useGetContentAttachment } from '@/composables/attachment-download/'
-import { IsCustomTopNet, IsSunPublishContent } from '@/views/article-content/composables/useContentStore'
+import {
+  IsCustomTopNet,
+  IsSunPublishContent
+} from '@/views/article-content/composables/useContentStore'
 
 export default {
   name: 'AttachmentDownload',
@@ -341,12 +344,24 @@ export default {
         }
 
         if (downUrl && fileUrl) {
-          location.href = fileUrl
+          this._downloadFile(fileUrl, result.fileName)
         } else {
           console.log('获取附件fid失败')
         }
       }
     },
+    _downloadFile(url, filename) {
+      if (!url) return
+      const link = document.createElement('a')
+      link.style.display = 'none'
+      link.href = url
+      // 同源download才会生效
+      link.setAttribute('download', filename)
+      document.body.appendChild(link)
+      link.click()
+      document.body.removeChild(link)
+      return link
+    },
     async getAttachmentInfo(file) {
       this.loading = true
       if (!this.loading) return

+ 15 - 1
apps/mobile/src/composables/attachment-download/component/AttachmentDownload.vue

@@ -396,11 +396,13 @@ export default {
                 path: '/static/open_in_browser',
                 query: {
                   type: 'attachment',
+                  filename: result.fileName,
                   target: encodeURIComponent(fileUrl)
                 }
               })
             } else {
-              location.href = fileUrl
+              // location.href = fileUrl
+              this._downloadFile(fileUrl, result.fileName)
             }
           } else {
             console.log('获取附件fid失败')
@@ -408,6 +410,18 @@ export default {
         }
       }
     },
+    _downloadFile(url, filename) {
+      if (!url) return
+      const link = document.createElement('a')
+      link.style.display = 'none'
+      link.href = url
+      // 同源download才会生效
+      link.setAttribute('download', filename)
+      document.body.appendChild(link)
+      link.click()
+      document.body.removeChild(link)
+      return link
+    },
     async getAttachmentInfo(file) {
       this.loading = true
       if (!this.loading) return

+ 21 - 2
apps/mobile/src/views/static/OpenInBrowser.vue

@@ -66,6 +66,7 @@ export default {
       },
       openType: '', // 类型
       target: '', // 外链地址
+      filename: '', // 指定文件名
       openInBrowserTip: false
     }
   },
@@ -81,9 +82,12 @@ export default {
   },
   methods: {
     getParams() {
-      const { type, target = '' } = this.$route.query
+      const { type, filename, target = '' } = this.$route.query
       this.openType = type
       this.target = decodeURIComponent(target)
+      if (filename) {
+        this.filename = filename
+      }
     },
     setHeaderTitle() {
       const { title } = this.$route.query
@@ -100,13 +104,28 @@ export default {
       const { actionEvent } = this.openConf
       actionEvent && actionEvent()
     },
+    _downloadFile(url, filename) {
+      if (!url) return
+      const link = document.createElement('a')
+      link.style.display = 'none'
+      link.href = url
+      // 同源download才会生效
+      if (filename) {
+        link.setAttribute('download', filename)
+      }
+      document.body.appendChild(link)
+      link.click()
+      document.body.removeChild(link)
+      return link
+    },
     attachmentAction() {
       const { inWx } = this.$envs
       if (inWx) {
         this.openInBrowserTip = true
       } else {
         if (this.target) {
-          location.href = this.target
+          // location.href = this.target
+          this._downloadFile(this.target, this.filename)
         }
       }
     }