ExportUtil.ts
7.01 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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
import {AxiosResponse, AxiosStatic} from "axios";
/**
* AcceptController
*/
//导出excel的工具类
export class ExportUtil {
/**
* post方式下载excel流式文件,一般用于多条件查询下载列表
* @param data
* @param fileName
*/
static downloadExcelFile(data: any, fileName: string) {
const blob = new Blob([data], {type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'});
const url = window.URL.createObjectURL(blob);
// 以动态创建a标签进行下载
const a = document.createElement('a');
a.href = url;
a.download = fileName;
a.click();
window.URL.revokeObjectURL(url);
}
/**
* get方式下载,一般全用的是固定条件
* @param url
*/
static downloadGetFile(url: string): void {
var a = document.createElement('a');
a.href = encodeURI(url);
a.setAttribute('target', '_blank');
a.click();
}
static downloadPng(data: any, fileName: string, type) {
// 处理AJAX响应
const blob = new Blob([data], {type: type}); // Assuming the image is PNG, adjust the type accordingly
const url = URL.createObjectURL(blob);
// 创建下载链接
const downloadLink = document.createElement('a');
downloadLink.href = url;
downloadLink.download = fileName; // 设置下载文件的名称
// 模拟点击下载链接
downloadLink.click();
}
static downloadExcel(axios: AxiosStatic, url: string,) {
//console.log("downloadExcel", url)
// 发起一个 GET 请求来获取 Excel 文件
axios.get(url, {
responseType: 'blob' // 指定响应数据类型为二进制流
}).then(response => {
this.downloadExcelFileNew(response);
})
.catch(error => {
console.error('Error downloading Excel file:', error);
});
}
public static downloadExcelFileNew(response: AxiosResponse<any>) {
// 从响应头中获取文件名
const fileNameEncode = response.headers['content-disposition']
.split('filename=')[1]
.split(';')[0];
const fileName = decodeURI(fileNameEncode).replaceAll('"', '');
//console.log(fileName)
// 创建一个 blob 对象,并将响应的二进制数据放入其中
const blob = new Blob([response.data], {type: 'application/vnd.ms-excel'});
// 创建一个 a 标签,并将 blob 对象的 URL 作为其 href 属性值
const link = document.createElement('a');
link.href = window.URL.createObjectURL(blob);
// 设置 a 标签的下载属性为要下载的文件名
link.download = fileName;
// 将 a 标签添加到页面中
document.body.appendChild(link);
// 触发点击事件,启动下载
link.click();
// 下载完成后,移除该 a 标签
document.body.removeChild(link);
}
static downloadWordFileNew(response: AxiosResponse<any>) {
// 从响应头中获取文件名
const fileNameEncode = response.headers['content-disposition']
.split('filename=')[1]
.split(';')[0];
const fileName = decodeURI(fileNameEncode).replaceAll('"', '');
//console.log(fileName)
// 创建一个 blob 对象,并将响应的二进制数据放入其中
const blob = new Blob([response.data], {type: 'application/msword'});
// 创建一个 a 标签,并将 blob 对象的 URL 作为其 href 属性值
const link = document.createElement('a');
link.href = window.URL.createObjectURL(blob);
// 设置 a 标签的下载属性为要下载的文件名
link.download = fileName;
// 将 a 标签添加到页面中
document.body.appendChild(link);
// 触发点击事件,启动下载
link.click();
// 下载完成后,移除该 a 标签
document.body.removeChild(link);
}
static downloadPPTFileNew(response: AxiosResponse<any>) {
// 从响应头中获取文件名
const fileNameEncode = response.headers['content-disposition']
.split('filename=')[1]
.split(';')[0];
const fileName = decodeURI(fileNameEncode).replaceAll('"', '');
//console.log(fileName)
// 创建一个 blob 对象,并将响应的二进制数据放入其中
const blob = new Blob([response.data], {type: 'application/msppt'});
// 创建一个 a 标签,并将 blob 对象的 URL 作为其 href 属性值
const link = document.createElement('a');
link.href = window.URL.createObjectURL(blob);
// 设置 a 标签的下载属性为要下载的文件名
link.download = fileName;
// 将 a 标签添加到页面中
document.body.appendChild(link);
// 触发点击事件,启动下载
link.click();
// 下载完成后,移除该 a 标签
document.body.removeChild(link);
}
static downloadPdfFileNew(response: AxiosResponse<any>) {
// 从响应头中获取文件名
const fileNameEncode = response.headers['content-disposition']
.split('filename=')[1]
.split(';')[0];
const fileName = decodeURI(fileNameEncode).replaceAll('"', '');
//console.log(fileName)
// 创建一个 blob 对象,并将响应的二进制数据放入其中
const blob = new Blob([response.data], {type: 'application/pdf'});
// 创建一个 a 标签,并将 blob 对象的 URL 作为其 href 属性值
const link = document.createElement('a');
link.href = window.URL.createObjectURL(blob);
// 设置 a 标签的下载属性为要下载的文件名
link.download = fileName;
// 将 a 标签添加到页面中
document.body.appendChild(link);
// 触发点击事件,启动下载
link.click();
// 下载完成后,移除该 a 标签
document.body.removeChild(link);
}
/**
* 下载远程文件
* @param url 文件的远程地址
* @param filename 下载时的文件名
*/
static async downloadFile(url: string, filename: string): Promise<void> {
try {
const response = await fetch(url);
if (!response.ok) {
throw new Error(`下载失败: ${response.statusText}`);
}
// 创建 Blob 对象
const blob = await response.blob();
const downloadUrl = URL.createObjectURL(blob);
// 创建一个隐藏的 a 标签模拟点击下载
const link = document.createElement("a");
link.href = downloadUrl;
link.download = filename;
document.body.appendChild(link);
link.click();
// 清理
document.body.removeChild(link);
URL.revokeObjectURL(downloadUrl);
} catch (error) {
console.error("文件下载失败:", error);
}
}
}