From 0375fd319c9f3b08d255c814cb0f8521d8ec641b Mon Sep 17 00:00:00 2001 From: 疯狂的狮子Li <15040126243@163.com> Date: 星期三, 18 八月 2021 11:11:24 +0800 Subject: [PATCH] !78 同步dev分支 Merge pull request !78 from 疯狂的狮子Li/dev --- ruoyi-ui/src/utils/download.js | 91 +++++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 91 insertions(+), 0 deletions(-) diff --git a/ruoyi-ui/src/utils/download.js b/ruoyi-ui/src/utils/download.js new file mode 100644 index 0000000..52b07f7 --- /dev/null +++ b/ruoyi-ui/src/utils/download.js @@ -0,0 +1,91 @@ +import axios from 'axios' +import { getToken } from '@/utils/auth' + +const mimeMap = { + xlsx: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', + zip: 'application/zip', + oss: 'application/octet-stream' +} + +const baseUrl = process.env.VUE_APP_BASE_API +export function downLoadZip(str, filename) { + var url = baseUrl + str + axios({ + method: 'get', + url: url, + responseType: 'blob', + headers: { 'Authorization': 'Bearer ' + getToken() } + }).then(res => { + 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)) { + if (value[key] !== null && typeof (value[key]) !== 'undefined') { + let params = propName + '[' + key + ']'; + let subPart = encodeURIComponent(params) + '='; + urlparams += subPart + encodeURIComponent(value[key]) + '&'; + } + } + } else { + urlparams += part + encodeURIComponent(value) + "&"; + } + } + } + urlparams = urlparams.slice(0, -1); + url = urlparams; + } + url = baseUrl + url + axios({ + method: 'get', + url: url, + responseType: 'blob', + headers: { 'Authorization': 'Bearer ' + getToken() } + }).then(res => { + resolveBlob(res, mimeMap.xlsx) + }) +} + +/** + * 瑙f瀽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 }) + // //浠巖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.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); +} -- Gitblit v1.9.3