mirror of
https://codeup.aliyun.com/67762337eccfc218f6110e0e/vue/fe-manage.git
synced 2025-12-06 17:36:44 +08:00
50 lines
1.4 KiB
JavaScript
50 lines
1.4 KiB
JavaScript
import axios from 'axios'
|
|
import {getCookieForName} from "@/api/method";
|
|
|
|
const mimeMap = {
|
|
xlsx: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
|
|
zip: 'application/zip'
|
|
}
|
|
|
|
const baseUrl = process.env.VUE_APP_ACT_API
|
|
export function downLoadZip(str, filename) {
|
|
var url = baseUrl + str
|
|
axios({
|
|
method: 'get',
|
|
url: url,
|
|
responseType: 'blob',
|
|
headers: { 'Authorization': 'Bearer ' + getCookieForName("token") }
|
|
}).then(res => {
|
|
resolveBlob(res, mimeMap.zip,filename)
|
|
})
|
|
}
|
|
const baseUrlManage = process.env.VUE_APP_BASE_API
|
|
export function downLoadZipManage(str, filename,fun) {
|
|
var url = baseUrlManage + str
|
|
axios({
|
|
method: 'get',
|
|
url: url,
|
|
responseType: 'blob',
|
|
headers: { 'Authorization': 'Bearer ' + getCookieForName("token") }
|
|
}).then(res => {
|
|
resolveBlob(res, mimeMap.xlsx,filename)
|
|
if(fun) fun()
|
|
})
|
|
}
|
|
/**
|
|
* 解析blob响应内容并下载
|
|
* @param {*} res blob响应内容
|
|
* @param {String} mimeType MIME类型
|
|
*/
|
|
export function resolveBlob(res, mimeType,filename) {
|
|
const link = document.createElement('a');// 创建a标签
|
|
let blob = new Blob([res.data], { type: mimeType }); // 设置文件类型
|
|
link.style.display = "none";
|
|
link.href = URL.createObjectURL(blob); // 创建URL
|
|
link.setAttribute("download", `${filename}`);
|
|
document.body.appendChild(link);
|
|
link.click();
|
|
URL.revokeObjectURL(link.href);
|
|
document.body.removeChild(link);
|
|
}
|