mirror of
https://gitee.com/ssssssss-team/magic-boot.git
synced 2025-01-19 12:02:50 +08:00
下载功能封装
This commit is contained in:
parent
f9f449761b
commit
539329e638
73
data/magic-api/api/系统管理/文件管理/下载.ms
Normal file
73
data/magic-api/api/系统管理/文件管理/下载.ms
Normal file
@ -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()
|
@ -2,6 +2,7 @@ import request from '@/scripts/request'
|
|||||||
import { ElMessageBox, ElNotification } from 'element-plus'
|
import { ElMessageBox, ElNotification } from 'element-plus'
|
||||||
import global from '@/scripts/global'
|
import global from '@/scripts/global'
|
||||||
import { utils, writeFile } from 'xlsx'
|
import { utils, writeFile } from 'xlsx'
|
||||||
|
import { getToken } from '@/scripts/auth'
|
||||||
|
|
||||||
const common = {}
|
const common = {}
|
||||||
|
|
||||||
@ -159,6 +160,14 @@ common.getUrl = (url, data) => {
|
|||||||
return url
|
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() => {
|
// common.loadConfig = async() => {
|
||||||
// await request({
|
// await request({
|
||||||
// url: '/system/config/list'
|
// url: '/system/config/list'
|
||||||
|
Loading…
Reference in New Issue
Block a user