From bb6792423300edbc317fef63e0595563c9666b72 Mon Sep 17 00:00:00 2001
From: 疯狂的狮子li <15040126243@163.com>
Date: 星期五, 23 七月 2021 10:48:28 +0800
Subject: [PATCH] update OSS下载文件名改为后端传输

---
 ruoyi-oss/src/main/java/com/ruoyi/oss/enumd/CloudServiceEnumd.java        |    2 +-
 ruoyi-oss/src/main/java/com/ruoyi/system/controller/SysOssController.java |    6 ++++++
 ruoyi-oss/src/main/java/com/ruoyi/oss/entity/UploadResult.java            |    2 ++
 ruoyi-ui/src/utils/ossdownload.js                                         |   14 ++++++++++----
 ruoyi-ui/src/views/system/oss/index.vue                                   |    2 +-
 5 files changed, 20 insertions(+), 6 deletions(-)

diff --git a/ruoyi-oss/src/main/java/com/ruoyi/oss/entity/UploadResult.java b/ruoyi-oss/src/main/java/com/ruoyi/oss/entity/UploadResult.java
index 6d38882..cb4f620 100644
--- a/ruoyi-oss/src/main/java/com/ruoyi/oss/entity/UploadResult.java
+++ b/ruoyi-oss/src/main/java/com/ruoyi/oss/entity/UploadResult.java
@@ -6,6 +6,8 @@
 
 /**
  * 涓婁紶杩斿洖浣�
+ *
+ * @author Lion Li
  */
 @Data
 @NoArgsConstructor
diff --git a/ruoyi-oss/src/main/java/com/ruoyi/oss/enumd/CloudServiceEnumd.java b/ruoyi-oss/src/main/java/com/ruoyi/oss/enumd/CloudServiceEnumd.java
index 497af55..a668bf2 100644
--- a/ruoyi-oss/src/main/java/com/ruoyi/oss/enumd/CloudServiceEnumd.java
+++ b/ruoyi-oss/src/main/java/com/ruoyi/oss/enumd/CloudServiceEnumd.java
@@ -9,7 +9,7 @@
 /**
  * 浜戝瓨鍌ㄦ湇鍔″晢鏋氫妇
  *
- * @author LionLi
+ * @author Lion Li
  */
 @Getter
 @AllArgsConstructor
diff --git a/ruoyi-oss/src/main/java/com/ruoyi/system/controller/SysOssController.java b/ruoyi-oss/src/main/java/com/ruoyi/system/controller/SysOssController.java
index 7ebca5f..3791030 100644
--- a/ruoyi-oss/src/main/java/com/ruoyi/system/controller/SysOssController.java
+++ b/ruoyi-oss/src/main/java/com/ruoyi/system/controller/SysOssController.java
@@ -10,6 +10,7 @@
 import com.ruoyi.common.core.page.TableDataInfo;
 import com.ruoyi.common.enums.BusinessType;
 import com.ruoyi.common.exception.CustomException;
+import com.ruoyi.common.utils.file.FileUtils;
 import com.ruoyi.system.bo.SysOssQueryBo;
 import com.ruoyi.system.domain.SysOss;
 import com.ruoyi.system.service.ISysOssService;
@@ -29,6 +30,8 @@
 import javax.servlet.http.HttpServletResponse;
 import javax.validation.constraints.NotEmpty;
 import java.io.IOException;
+import java.net.URLEncoder;
+import java.nio.charset.StandardCharsets;
 import java.util.Arrays;
 import java.util.HashMap;
 import java.util.Map;
@@ -88,6 +91,9 @@
 			throw new CustomException("鏂囦欢鏁版嵁涓嶅瓨鍦�!");
 		}
 		response.reset();
+		response.addHeader("Access-Control-Allow-Origin", "*");
+		response.addHeader("Access-Control-Expose-Headers", "Content-Disposition");
+		FileUtils.setAttachmentResponseHeader(response, URLEncoder.encode(sysOss.getOriginalName(), StandardCharsets.UTF_8));
 		response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE + "; charset=UTF-8");
 		long data = HttpUtil.download(sysOss.getUrl(), response.getOutputStream(), false);
 		response.setContentLength(Convert.toInt(data));
diff --git a/ruoyi-ui/src/utils/ossdownload.js b/ruoyi-ui/src/utils/ossdownload.js
index e212ca8..0b9cf10 100644
--- a/ruoyi-ui/src/utils/ossdownload.js
+++ b/ruoyi-ui/src/utils/ossdownload.js
@@ -6,7 +6,7 @@
 }
 
 const baseUrl = process.env.VUE_APP_BASE_API
-export function downLoadOss(ossId, filename) {
+export function downLoadOss(ossId) {
   var url = baseUrl + '/system/oss/download/' + ossId
     axios({
     method: 'get',
@@ -14,7 +14,7 @@
     responseType: 'blob',
     headers: { 'Authorization': 'Bearer ' + getToken() }
   }).then(res => {
-    resolveBlob(res, mimeMap.oss, filename)
+    resolveBlob(res, mimeMap.oss)
   })
 }
 /**
@@ -22,11 +22,17 @@
  * @param {*} res blob鍝嶅簲鍐呭
  * @param {String} mimeType MIME绫诲瀷
  */
-export function resolveBlob(res, mimeType, filename) {
+export function resolveBlob(res, mimeType) {
   const aLink = document.createElement('a')
   var blob = new Blob([res.data], { type: mimeType })
+  // 浠巖esponse鐨刪eaders涓幏鍙杅ilename, 鍚庣response.setHeader("Content-disposition", "attachment; filename=xxxx.docx") 璁剧疆鐨勬枃浠跺悕;
+  var patt = new RegExp('filename=([^;]+\\.[^\\.;]+);*')
+  var contentDisposition = decodeURI(res.headers['content-disposition'])
+  var result = patt.exec(contentDisposition)
+  var fileName = result[1]
+  fileName = fileName.replace(/\"/g, '')
   aLink.href = URL.createObjectURL(blob)
-  aLink.setAttribute('download', filename) // 璁剧疆涓嬭浇鏂囦欢鍚嶇О
+  aLink.setAttribute('download', decodeURI(fileName)) // 璁剧疆涓嬭浇鏂囦欢鍚嶇О
   document.body.appendChild(aLink)
   aLink.click()
   document.body.removeChild(aLink);
diff --git a/ruoyi-ui/src/views/system/oss/index.vue b/ruoyi-ui/src/views/system/oss/index.vue
index 12bc5d3..5ce6575 100644
--- a/ruoyi-ui/src/views/system/oss/index.vue
+++ b/ruoyi-ui/src/views/system/oss/index.vue
@@ -289,7 +289,7 @@
     },
     /** 涓嬭浇鎸夐挳鎿嶄綔 */
     handleDownload(row) {
-      downLoadOss(row.ossId, row.originalName)
+      downLoadOss(row.ossId)
     },
     /** 鍒犻櫎鎸夐挳鎿嶄綔 */
     handleDelete(row) {

--
Gitblit v1.9.3