zipdownload.js
9.28 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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
import axios from "axios";
import { getToken } from "@/utils/auth";
import settings from "@/settings";
// const mimeMap = {
// xlsx: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
// zip: "application/zip",
// pdf: "application/pdf",
// };
const mimeMap = {
xlsx: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
zip: 'application/zip',
pdf: 'application/pdf',
jpg: 'image/jpeg',
jpeg: 'image/jpeg',
png: 'image/png',
bmp: 'image/bmp',
tiff: 'image/tiff',
tif: 'image/tiff',
xls: 'application/vnd.ms-excel',
docx: 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
doc: 'application/msword'
}
const baseUrl = process.env.VUE_APP_BASE_API; //接口地址
export function downLoadZip(str, data, filename) {
var url = baseUrl + str;
return new Promise((resolve, reject) => {
axios({
method: "get",
url: url,
params: {
cusFileId: data,
},
responseType: "blob",
headers: { Authorization: getToken() },
})
.then((res) => {
resolveBlob(res, mimeMap.zip, filename);
resolve(res);
})
.catch((err) => {
reject(err);
});
});
}
//异步上传报关文件
export function downLoadViewPdf(str, id, title) {
var url = baseUrl + str;
return new Promise((resolve, reject) => {
axios({
method: "post",
url: url,
params: id, // 参数
responseType: "blob",
headers: { Authorization: getToken() },
})
.then((res) => {
const blob = new Blob([res.data], {
type: "application/pdf",
});
let url = window.URL.createObjectURL(blob);
var win= window.open(url, "_blank"); // 打开新的标签页
var setTitle= setInterval(() => {
if (win.document.title!=""){
clearInterval("setTitle");
}else{
win.document.title = title;
}
},500);
// var fileURL = URL.createObjectURL(blob);
// var win = window.open();
// win.document.write('<iframe name="' + title + '" src="' + fileURL + '" frameborder="0" style="border:0; top:0px; left:0px; bottom:0px; right:0px; width:100%; height:100%;" allowfullscreen></iframe>');
// win.document.title = title;
resolve(url);
})
.catch((err) => {
reject(err);
});
});
}
// export function downLoadViewPdf(str, id,title) {
// var url = baseUrl + str;
// axios({
// url: url,
// method: "post",
// params: id, // 参数
// responseType: "blob",
// headers: { Authorization: getToken() },
// }).then((res) => {
// const blob = new Blob([res.data], {
// type: "application/pdf",
// });
// // const aLink = document.createElement("a");
// // aLink.href = URL.createObjectURL(res.data);
// // aLink.setAttribute("download", "test.pdf"); // 设置下载文件名称
// // document.body.appendChild(aLink);
// // aLink.click();
// // document.body.removeChild(aLink);
// // var fileURL = URL.createObjectURL(blob);
// // var win = window.open();
// // win.document.write('<iframe name="' + title + '" src="' + fileURL + '" frameborder="0" style="border:0; top:0px; left:0px; bottom:0px; right:0px; width:100%; height:100%;" allowfullscreen></iframe>');
// // win.document.title = title;
// // console.log(fileURL);
// // console.log(win);
// // let url = window.URL.createObjectURL(blob);
// // var win= window.open(url, "_blank"); // 打开新的标签页
// // //关闭查看按钮得loading
// // data.findLoading=false;
// // var setTitle= setInterval(() => {
// // if (win.document.title!=""){
// // clearInterval("setTitle");
// // }else{
// // win.document.title = title;
// // }
// // },500);
// let url1 = window.URL.createObjectURL(blob);
// console.log(url1);
// const newWindow = window.open('about:blank', title);
// newWindow.document.title = title;
// let iframe = document.createElement('iframe');
// iframe.src = url1;
// iframe.style.width = '100%';
// iframe.style.height = '100vh';
// iframe.style.margin = '0';
// iframe.style.padding = '0';
// iframe.style.overflow = 'hidden';
// iframe.style.border = 'none';
// newWindow.document.body.style.margin = '0';
// newWindow.document.body.appendChild(iframe);
// });
// }
export function downLoadPdf(str, id, name) {
var url = baseUrl + str;
return new Promise((resolve, reject) => {
axios({
url: url,
method: "post",
params: id, // 参数
responseType: "arraybuffer",
headers: { Authorization: getToken() },
})
.then((res) => {
const blob = new Blob([res.data], {
type: "application/pdf",
});
let url = window.URL.createObjectURL(blob);
const link = document.createElement("a");
link.style.display = "none";
link.href = url;
link.setAttribute("download", name);
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
resolve(res);
})
.catch((err) => {
reject(err);
});
});
}
/**
* 解析blob响应内容并下载
* @param {*} res blob响应内容
* @param {String} mimeType MIME类型
*/
export function resolveBlob(res, mimeType, fileName) {
const aLink = document.createElement("a");
var blob = new Blob([res.data], { 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]
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,
responseType: "blob",
headers: { Authorization: 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: 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);
}
export function downLoadFile(str, data, filename) {
// var url = baseUrl + str;
// const aLink = document.createElement("a");
// aLink.href = URL.createObjectURL(data);
// aLink.setAttribute("download", filename); // 设置下载文件名称
// document.body.appendChild(aLink);
// aLink.click();
// document.body.removeChild(aLink);
var url = baseUrl + str;
return new Promise((resolve, reject) => {
axios({
method: "get",
url: url,
params: {
fileId: data,
},
responseType: "blob",
headers: { Authorization: getToken() },
})
.then((res) => {
const aLink = document.createElement("a");
aLink.href = URL.createObjectURL(res.data);
aLink.setAttribute("download", filename); // 设置下载文件名称
document.body.appendChild(aLink);
aLink.click();
document.body.removeChild(aLink);
// resolveBlob(res, mimeMap.pdf, filename);
resolve(res);
// console.log(type.toLowerCase());
// resolveBlob(res, mimeMap[type.toLowerCase()], filename, type.toLowerCase())
})
.catch((err) => {
reject(err);
});
});
}
//异步上传报关文件
export function uploadCustomsFileAsync(str, data) {
var url = baseUrl + str;
return new Promise((resolve, reject) => {
axios({
method: "post",
url: url,
data: data,
responseType: "blob",
headers: { Authorization: getToken() },
})
.then((res) => {
resolve(res);
})
.catch((err) => {
reject(err);
});
});
}