zipdownload.js
3.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
import axios from 'axios'
import { getToken } from '@/utils/auth'
const mimeMap = {
xlsx: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
zip: 'application/zip'
}
const baseUrl = 'https://imacuat.zmd.apac.fedex.com/iMAC' //uat服务器
// const baseUrl = 'http://47.103.140.98:7001/IMAC2' //阿里云服务器
// const baseUrl = 'http://192.168.1.133:7001/IMAC2' //测试服务器
export function downLoadZip(str, filename) {
var url = baseUrl + str
axios({
method: 'get',
url: url,
responseType: 'blob',
headers: { 'Authorization': decodeURIComponent(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], { 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.removeChild(aLink);
}
// excel导出请求
export function downLoadXlsx(str, data,fileName) {
var url = baseUrl + str
return new Promise((resolve,reject)=>{
axios({
method: 'post',
url: url,
data: data,
timeout:500000,
responseType: 'blob',
headers: { 'Authorization': decodeURIComponent(getToken()) }
// headers: { 'Authorization': decodeURIComponent(getToken()) }
//uat环境token需要做url解码处理
}).then(res => {
if (res.status === 200) {
downLoadExcal(res,fileName)
}
resolve(res)
}).catch((err)=>{
reject(err)
})
})
}
//excel导出下载
export function downLoadExcal(val,filename) {
const aLink = document.createElement('a')
const fileName = filename+".xlsx"
aLink.href = URL.createObjectURL(val.data)
aLink.setAttribute('download', fileName) // 设置下载文件名称
document.body.appendChild(aLink)
aLink.click()
document.body.removeChild(aLink);
}
//航班预审导出
export function cargoXlsxPre(str, data) {
var url = baseUrl + str
return new Promise((resolve,reject)=>{
axios({
method: 'post',
url: url,
data: data,
responseType: 'blob',
headers: { 'Authorization': decodeURIComponent(getToken()) }
}).then(res => {
if (res.status === 200) {
cargoTableXlsxPre(res)
}
resolve()
}).catch((err)=>{
reject(err)
})
})
}
//航班预审导出
export function cargoTableXlsxPre(val) {
const aLink = document.createElement('a')
const fileName = "航班预审表.xlsx"
aLink.href = URL.createObjectURL(val.data)
aLink.setAttribute('download', fileName) // 设置下载文件名称
document.body.appendChild(aLink)
aLink.click()
document.body.removeChild(aLink);
}