From 539329e63827ed30f1b14e45b78847fa37aa5fc9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=90=95=E9=87=91=E6=B3=BD?= <1098696801@qq.com> Date: Fri, 24 Jun 2022 14:15:07 +0800 Subject: [PATCH] =?UTF-8?q?=E4=B8=8B=E8=BD=BD=E5=8A=9F=E8=83=BD=E5=B0=81?= =?UTF-8?q?=E8=A3=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- data/magic-api/api/系统管理/文件管理/下载.ms | 73 ++++++++++++++++++++ magic-boot-ui/src/scripts/common.js | 9 +++ 2 files changed, 82 insertions(+) create mode 100644 data/magic-api/api/系统管理/文件管理/下载.ms diff --git a/data/magic-api/api/系统管理/文件管理/下载.ms b/data/magic-api/api/系统管理/文件管理/下载.ms new file mode 100644 index 0000000..6dbe9fb --- /dev/null +++ b/data/magic-api/api/系统管理/文件管理/下载.ms @@ -0,0 +1,73 @@ +{ + "properties" : { }, + "id" : "63f76dedb8084db7b11f45b2ef1285de", + "script" : null, + "groupId" : "fd3d225a1cf141bf9998c4ec4bf4a6ab", + "name" : "下载", + "createTime" : null, + "updateTime" : 1656050052797, + "lock" : null, + "createBy" : null, + "updateBy" : null, + "path" : "/download", + "method" : "GET", + "parameters" : [ { + "name" : "urls", + "value" : "userfiles/2022-05-05/c6aeb598deef48c385b0da831b7c2b85/QQ%E5%9B%BE%E7%89%8720220505165410.jpg,userfiles/2022-06-24/3eae989f6e5942a3b2e3dab2653865e3/QQ%E5%9B%BE%E7%89%8720220519110559.jpg", + "description" : null, + "required" : false, + "dataType" : "String", + "type" : null, + "defaultValue" : null, + "validateType" : null, + "error" : null, + "expression" : null, + "children" : null + } ], + "options" : [ ], + "requestBody" : "", + "headers" : [ ], + "paths" : [ ], + "responseBody" : null, + "description" : null, + "requestBodyDefinition" : null, + "responseBodyDefinition" : null +} +================================ +import java.net.URLDecoder +import java.net.URLEncoder +import java.io.File +import org.ssssssss.magicboot.model.Global +import cn.hutool.core.io.FileUtil +import cn.hutool.core.util.CharsetUtil +import cn.hutool.core.util.ZipUtil +import response + +var files = [] +urls = URLDecoder.decode(urls, "UTF-8").split(',') +for(url in urls){ + files.push(FileUtil.file(Global.getUserFilesBaseDir() + "/" + url)) +} +if(!filename){ + if(urls.length > 1){ + filename = '附件.zip' + }else{ + filename = urls[0].substring(urls[0].lastIndexOf('/') + 1) + } +}else{ + if(urls.length > 1){ + filename += '.zip' + }else{ + filename += urls[0].substring(urls[0].lastIndexOf('.')) + } +} +response.setHeader("Content-Type","application/octet-stream;charset=utf-8") +response.setHeader("Content-Disposition", `attachment;filename=${URLEncoder.encode(filename, "UTF-8")}`) + +if(urls.length > 1){ + ZipUtil.zip(response.getOutputStream(), CharsetUtil.defaultCharset(), false, null, files.toArray(new_array(File.class, 0))); +}else{ + FileUtil.writeToStream(FileUtil.file(Global.getUserFilesBaseDir() + "/" + urls[0]), response.getOutputStream()) +} + +response.end() \ No newline at end of file diff --git a/magic-boot-ui/src/scripts/common.js b/magic-boot-ui/src/scripts/common.js index 1a3bfda..645d104 100644 --- a/magic-boot-ui/src/scripts/common.js +++ b/magic-boot-ui/src/scripts/common.js @@ -2,6 +2,7 @@ import request from '@/scripts/request' import { ElMessageBox, ElNotification } from 'element-plus' import global from '@/scripts/global' import { utils, writeFile } from 'xlsx' +import { getToken } from '@/scripts/auth' const common = {} @@ -159,6 +160,14 @@ common.getUrl = (url, data) => { return url } +common.download = (urls, filename) => { + location.href = common.downloadHref(urls, filename) +} + +common.downloadHref = (urls, filename) => { + return global.baseApi + `/system/file/download?urls=${encodeURI(urls)}&filename=${filename || ''}&token=${getToken()}` +} + // common.loadConfig = async() => { // await request({ // url: '/system/config/list'