Skip to content

Commit f98e69a

Browse files
author
woytu
committed
优化文件下载
1 parent 13a4d11 commit f98e69a

File tree

2 files changed

+142
-28
lines changed

2 files changed

+142
-28
lines changed

static/js/index.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,6 @@ function getKey() {
8080
contentType: "application/x-www-form-urlencoded; charset=UTF-8",
8181
responseType: "blob",
8282
success: function (result, status, xhr) {
83-
console.log(xhr.getAllResponseHeaders());
8483
// 从response的headers中获取filename, 后端response.setHeader("Content-Disposition", "attachment; filename=xxxx.xxx") 设置的文件名;
8584
let contentDisposition = xhr.getResponseHeader('Content-Disposition');
8685
let patt = new RegExp("filename=([^;]+\\.[^\\.;]+);*");
@@ -93,8 +92,7 @@ function getKey() {
9392
filename = patt.exec(contentDisposition)[1];
9493
}
9594
// 取文件名信息中的文件名,替换掉文件名中多余的符号
96-
filename = replace(filename, "\\\\", "", true);
97-
filename = replace(filename, "/", "", true);
95+
filename = filename.replaceAll("\\\\|/|\"", "");
9896

9997
let downloadElement = document.createElement('a');
10098
downloadElement.style.display = 'none';

static/js/util.js

Lines changed: 141 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,91 @@
11
/**
2-
groovy/lang/GroovyShell
2+
* 给String对象增加一个原型方法:
3+
* 判断一个字符串是以指定字符串结尾的
4+
*
5+
* @param endStr 需要判断的子字符串
6+
* @return boolean 是否以该字符串结尾
7+
* @Description
8+
* @author claer woytu.com
9+
* @date 2019/5/24 11:22
10+
*/
11+
String.prototype.endWith = function (str) {
12+
if (str == null || str == "" || this.length == 0 || str.length > this.length)
13+
return false;
14+
if (this.substring(this.length - str.length) != str) {
15+
return false;
16+
}
17+
return true;
18+
}
19+
/**
20+
* 给String对象增加一个原型方法:
21+
* 判断一个字符串是以指定字符串开头的
22+
*
23+
* @param endStr 需要判断的子字符串
24+
* @return boolean 是否以该字符串开头
25+
* @Description
26+
* @author claer woytu.com
27+
* @date 2019/5/24 11:22
28+
*/
29+
String.prototype.startWith = function (str) {
30+
if (str == null || str == "" || this.length == 0 || str.length > this.length)
31+
return false;
32+
if (this.substr(0, str.length) != str) {
33+
return false;
34+
}
35+
return true;
36+
}
37+
38+
/**
39+
* 给String对象增加一个原型方法:
40+
* 判断一个字符串是以指定字符串结尾的
41+
*
42+
* @param endStr 需要判断的子字符串
43+
* @return boolean 是否以该字符串结尾
44+
* @Description
45+
* @author claer woytu.com
46+
* @date 2019/5/24 11:22
47+
*/
48+
String.prototype.endWithRegExp = function (str) {
49+
let reg = new RegExp(str + "$");
50+
return reg.test(this);
51+
}
52+
/**
53+
* 给String对象增加一个原型方法:
54+
* 判断一个字符串是以指定字符串开头的
55+
*
56+
* @param endStr 需要判断的子字符串
57+
* @return boolean 是否以该字符串开头
58+
* @Description
59+
* @author claer woytu.com
60+
* @date 2019/5/24 11:22
61+
*/
62+
63+
String.prototype.startWithRegExp = function (str) {
64+
let reg = new RegExp("^" + str);
65+
return reg.test(this);
66+
}
67+
68+
69+
/**
70+
* 给String对象增加一个原型方法:
71+
* 替换全部字符串 - 无replaceAll的解决方案,自定义扩展js函数库
72+
* 原生js中并没有replaceAll方法,只有replace,如果要将字符串替换,一般使用replace
73+
*
74+
* @param FindText 要替换的字符串
75+
* @param RepText 新的字符串
76+
* @return string
77+
* @Description
78+
* @author claer woytu.com
79+
* @date 2019/5/24 15:24
80+
*/
81+
String.prototype.replaceAll = function (FindText, RepText) {
82+
// g表示执行全局匹配,m表示执行多次匹配
83+
let regExp = new RegExp(FindText, "gm");
84+
return this.replace(regExp, RepText);
85+
}
86+
87+
88+
/**
389
* @return
490
* @Description 获取当前路径
591
* @author claer woytu.com
@@ -164,62 +250,92 @@ const reinsertElement = (array, element) => {
164250
}
165251

166252

253+
/**
254+
* 设置延时后再执行下一步操作
255+
*
256+
* @return
257+
* @Description
258+
* @author claer woytu.com
259+
* @date 2019/7/4 20:22
260+
*/
261+
const delay = ms => new Promise(resolve => setTimeout(resolve, ms));
262+
263+
167264
/**
168265
* 判断js数组/对象是否为空
169266
* isPrototypeOf() 验证一个对象是否存在于另一个对象的原型链上。即判断 Object 是否存在于 $obj 的原型链上。
170267
* js中一切皆对象,也就是说,Object 也存在于数组的原型链上,因此这里数组需要先于对象检验。
171268
* Object.keys() 返回一个由给定对象的自身可枚举属性组成的数组,数组中属性名的排列顺序和使用 for...in 循环遍历该对象时返回的顺序一致
172269
* @param $obj
173270
* @return {boolean}
174-
* @Description
175-
* @author claer woytu.com
176-
* @date 2019/4/29 20:12
177271
*/
178-
const isEmpty = ($obj) => {
272+
function isEmpty($obj) {
179273
// 找不到属性
180-
if (typeof $obj == 'undefined') {
274+
if (typeof ($obj) == 'undefined') {
181275
return true;
182276
}
183277
// 检验非数组/对象类型 EX:undefined null '' 根据自身要求添加其他适合的为空的值 如:0 ,'0',' ' 等
184278
if ($obj === 0 || $obj === '' || $obj === null) {
185279
return true;
186280
}
187-
if (typeof $obj === "string") {
188-
$obj = $obj.trim().replace(/\s*/g, ""); //移除字符串中所有 ''
281+
if (typeof ($obj) === "string") {
282+
$obj = $obj.replace(/\s*/g, ""); //移除字符串中所有 ''
189283
if ($obj === '') {
190284
return true;
191285
}
192-
} else if (typeof $obj === "object") {
193-
if (!Array.isArray($obj) || $obj.length <= 0) {
194-
return true;
195-
}
196-
if (!Object.prototype.isPrototypeOf($obj) || !Object.keys($obj).length != 0) {
286+
}
287+
if (typeof ($obj) === "object") {
288+
if (!Array.isArray($obj) || $obj.length <= 0 || Object.keys($obj).length <= 0) {
197289
return true;
198290
}
199291
}
200292
return false;
201293
}
202294

295+
203296
/**
204-
* replace默认只替换第一个匹配项
205-
* @param str 父字符串
206-
* @param substring 被替换的字符串
207-
* @param newString 新字符串
297+
* 过滤在数组中的值
208298
*
209-
* "g"是匹配全部的意思,也可以换成"",就是匹配第一个
299+
* @param arr 元数据数组
300+
* @param ignoresArr 需要去除的值数组
301+
* @return Array 去掉值后的新数组
302+
* @Description
303+
* @author claer woytu.com
304+
* @date 2019/5/23 16:30
305+
*/
306+
function inArrayKV(arr, ignoresArr) {
307+
let newArr = [];
308+
arr.forEach(function (value, index, array) {
309+
// 判断文件名以什么开头、是否在指定数组中存在
310+
if (!value.startsWith(".") && ignoresArr.includes(value)) {
311+
newArr.push(value);
312+
}
313+
});
314+
return newArr;
315+
}
316+
317+
/**
318+
* 过滤不在数组中的值
210319
*
211-
* @return
320+
* @param arr 元数据数组
321+
* @param retentionArr 需要保留的值数组
322+
* @return Array 去掉值后的新数组
212323
* @Description
213324
* @author claer woytu.com
214-
* @date 2019/4/30 15:20
325+
* @date 2019/5/23 16:30
215326
*/
216-
const replace = (str, substring, newString, isAll) => {
217-
if (!isEmpty(isAll) && isAll) {
218-
return str.replace(new RegExp(substring, "g"), newString);
219-
}
220-
return str.replace(new RegExp(substring, ""), newString);
327+
function notInArrayKV(arr, retentionArr) {
328+
let newArr = [];
329+
arr.forEach(function (value, index, array) {
330+
// 判断文件名以什么开头、是否在指定数组中存在
331+
if (!value.startsWith(".") && !retentionArr.includes(value)) {
332+
newArr.push(value);
333+
}
334+
});
335+
return newArr;
221336
}
222337

338+
223339
/**
224340
* 正则表达式去除空行
225341
*

0 commit comments

Comments
 (0)