常用业务代码

常用业务代码


1. 从后台下载文件

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
export async function downloadFile(url: string, filename = "file") {
axios({
url: url,
method: "get",
responseType: "blob",
})
.then((res) => {
/* 兼容ie内核,360浏览器的兼容模式 */
if (window.navigator && window.navigator["msSaveOrOpenBlob"]) {
const blob = new Blob([res.data]);
window.navigator["msSaveOrOpenBlob"](blob, filename);
} else {
const downloadElement = document.createElement("a");
const href = window.URL.createObjectURL(res.data);
downloadElement.href = href;
downloadElement.download = filename;
document.body.appendChild(downloadElement);
downloadElement.click();
document.body.removeChild(downloadElement);
window.URL.revokeObjectURL(href);
}
})
.catch((response) => {
console.log("err ==>", response);
});
}

2. 获取路由 url 上的参数

1
2
3
4
5
6
7
8
9
10
11
12
export function GetUrlParam(name) {
var url = window.location.href;
const params = url.substr(url.lastIndexOf("?") + 1).split("&");
for (let i = 0; i < params.length; i++) {
const param = params[i];
const key = param.split("=")[0];
const value = param.split("=")[1];
if (key === name) {
return value;
}
}
}

本博客所有文章除特别声明外,均采用 CC BY-SA 4.0 协议 ,转载请注明出处!