常用业务代码
常用业务代码
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) => { 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 上的参数
| 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; } } }
|