From 85900682b8e88cfbb5d0e8aa53d9b7b9067bbe18 Mon Sep 17 00:00:00 2001 From: zhanglipeng Date: Fri, 29 Jan 2021 12:55:18 +0800 Subject: [PATCH] =?UTF-8?q?=E4=B8=8B=E8=BD=BD=E6=96=87=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ruoyi-ui/src/utils/zipdownload.js | 107 +++++++++++------- .../src/views/benyi/child_files/index.vue | 31 +++-- .../framework/config/SecurityConfig.java | 6 +- .../project/common/CommonController.java | 6 + 4 files changed, 95 insertions(+), 55 deletions(-) diff --git a/ruoyi-ui/src/utils/zipdownload.js b/ruoyi-ui/src/utils/zipdownload.js index 3a8681468..4142b0982 100644 --- a/ruoyi-ui/src/utils/zipdownload.js +++ b/ruoyi-ui/src/utils/zipdownload.js @@ -1,40 +1,67 @@ -import axios from 'axios' -import { getToken } from '@/utils/auth' - -const mimeMap = { - xlsx: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', - zip: 'application/zip' -} - -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) - }) -} -/** - * 解析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.href = URL.createObjectURL(blob) - aLink.setAttribute('download', fileName) // 设置下载文件名称 - document.body.appendChild(aLink) - aLink.click() - document.body.appendChild(aLink) -} +import axios from 'axios' +import { getToken } from '@/utils/auth' + +const mimeMap = { + xlsx: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', + zip: 'application/zip' +} + +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 downLoadUrl(str, item) { + var url = baseUrl + str + axios({ + method: 'get', + url: url, + params: { name: item.fileurl }, + responseType: 'blob', + headers: { 'Authorization': 'Bearer ' + getToken() }, + }).then(res => { + console.log(res); + downloadFileFun(res, item) + }) +} + +export function downloadFileFun(data, item) { + if (!data) { + return + } + let url = window.URL.createObjectURL(new Blob([data])) + let link = document.createElement('a') + link.style.display = 'none' + link.href = url + link.setAttribute('download', item.name) + document.body.appendChild(link) + link.click() +} + +/** + * 解析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.href = URL.createObjectURL(blob) + aLink.setAttribute('download', fileName) // 设置下载文件名称 + document.body.appendChild(aLink) + aLink.click() + document.body.appendChild(aLink) +} diff --git a/ruoyi-ui/src/views/benyi/child_files/index.vue b/ruoyi-ui/src/views/benyi/child_files/index.vue index fecb9b6ea..5d37ca45c 100644 --- a/ruoyi-ui/src/views/benyi/child_files/index.vue +++ b/ruoyi-ui/src/views/benyi/child_files/index.vue @@ -12,13 +12,18 @@ --> -