From 89cca8af078ad110dbe20a81b2850de98b6f78d0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=96=AF=E7=8B=82=E7=9A=84=E7=8B=AE=E5=AD=90li?= <15040126243@163.com> Date: Tue, 3 Aug 2021 19:29:15 +0800 Subject: [PATCH] =?UTF-8?q?update=20=E9=87=8D=E6=9E=84=20=E7=BB=9F?= =?UTF-8?q?=E4=B8=80=E4=BD=BF=E7=94=A8=20zip=20=E5=B7=A5=E5=85=B7=E4=B8=8B?= =?UTF-8?q?=E8=BD=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ruoyi-ui/src/utils/ossdownload.js | 41 ----------------------- ruoyi-ui/src/utils/zipdownload.js | 54 +++++++++++++++++++++++++++++-- 2 files changed, 52 insertions(+), 43 deletions(-) delete mode 100644 ruoyi-ui/src/utils/ossdownload.js diff --git a/ruoyi-ui/src/utils/ossdownload.js b/ruoyi-ui/src/utils/ossdownload.js deleted file mode 100644 index 6da52bb7..00000000 --- a/ruoyi-ui/src/utils/ossdownload.js +++ /dev/null @@ -1,41 +0,0 @@ -import axios from 'axios' -import { getToken } from '@/utils/auth' - -const mimeMap = { - oss: 'application/octet-stream' -} - -const baseUrl = process.env.VUE_APP_BASE_API -export function downLoadOss(ossId) { - var url = baseUrl + '/system/oss/download/' + ossId - axios({ - method: 'get', - url: url, - responseType: 'blob', - headers: { 'Authorization': 'Bearer ' + getToken() } - }).then(res => { - resolveBlob(res, mimeMap.oss) - }) -} -/** - * 解析blob响应内容并下载 - * @param {*} res blob响应内容 - * @param {String} mimeType MIME类型 - */ -export function resolveBlob(res, mimeType) { - const aLink = document.createElement('a') - var blob = new Blob([res.data], { type: mimeType }) - // 从response的headers中获取filename, 后端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.style.display = 'none' - aLink.href = URL.createObjectURL(blob) - aLink.setAttribute('download', decodeURI(fileName)) // 设置下载文件名称 - document.body.appendChild(aLink) - aLink.click() - URL.revokeObjectURL(aLink.href);//清除引用 - document.body.removeChild(aLink); -} diff --git a/ruoyi-ui/src/utils/zipdownload.js b/ruoyi-ui/src/utils/zipdownload.js index 52afcc66..aa926030 100644 --- a/ruoyi-ui/src/utils/zipdownload.js +++ b/ruoyi-ui/src/utils/zipdownload.js @@ -3,7 +3,9 @@ import { getToken } from '@/utils/auth' const mimeMap = { xlsx: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', - zip: 'application/zip' + zip: 'application/zip', + excel: 'application/vnd.ms-excel', + oss: 'application/octet-stream' } const baseUrl = process.env.VUE_APP_BASE_API @@ -18,6 +20,54 @@ export function downLoadZip(str, filename) { resolveBlob(res, mimeMap.zip) }) } + +export function downLoadOss(ossId) { + var url = baseUrl + '/system/oss/download/' + ossId + axios({ + method: 'get', + url: url, + responseType: 'blob', + headers: { 'Authorization': 'Bearer ' + getToken() } + }).then(res => { + resolveBlob(res, mimeMap.oss) + }) +} + +export function downLoadExcel(url, params) { + // get请求映射params参数 + if (params) { + let urlparams = url + '?'; + for (const propName of Object.keys(params)) { + const value = params[propName]; + var part = encodeURIComponent(propName) + "="; + if (value !== null && typeof(value) !== "undefined") { + if (typeof value === 'object') { + for (const key of Object.keys(value)) { + let params = propName + '[' + key + ']'; + var subPart = encodeURIComponent(params) + "="; + urlparams += subPart + encodeURIComponent(value[key]) + "&"; + } + } else { + urlparams += part + encodeURIComponent(value) + "&"; + } + } + } + urlparams = urlparams.slice(0, -1); + params = {}; + url = urlparams; + } + url = baseUrl + url + axios({ + method: 'get', + url: url, + params: params, + responseType: 'blob', + headers: { 'Authorization': 'Bearer ' + getToken() } + }).then(res => { + resolveBlob(res, mimeMap.excel) + }) +} + /** * 解析blob响应内容并下载 * @param {*} res blob响应内容 @@ -34,7 +84,7 @@ export function resolveBlob(res, mimeType) { fileName = fileName.replace(/\"/g, '') aLink.style.display = 'none' aLink.href = URL.createObjectURL(blob) - aLink.setAttribute('download', fileName) // 设置下载文件名称 + aLink.setAttribute('download', decodeURI(fileName)) // 设置下载文件名称 document.body.appendChild(aLink) aLink.click() URL.revokeObjectURL(aLink.href);//清除引用