123456789101112131415161718192021222324252627282930313233343536373839 |
- import { httpApp } from "@/plugins/axiosApp";
- import router from "@/router";
- import { AxiosResponse } from "axios";
- // 下载文件
- export async function downloadFileURL(url: string, name?: string) {
- return httpApp
- .get(url, { responseType: "blob" })
- .then((res) => {
- return responseToFile(res);
- })
- .catch((err) => {});
- }
- export async function responseToFile(response: AxiosResponse) {
- return new Promise((resolve) => {
- const { data, headers } = response;
- const fileName = headers["content-disposition"].replace(
- /\w+;\s*filename=(.*)/,
- "$1"
- );
- //Here, when the JSON file is returned, the JSON.stringify Processing, other types of files do not need to be processed
- //const blob = new Blob([JSON.stringify(data)], ...)
- const blob = new Blob([data], { type: headers["content-type"] });
- let dom = document.createElement("a");
- let url = window.URL.createObjectURL(blob);
- dom.href = url;
- dom.download = decodeURI(fileName);
- dom.style.display = "none";
- document.body.appendChild(dom);
- dom.click();
- dom.parentNode?.removeChild(dom);
- window.URL.revokeObjectURL(url);
- });
- }
- // 返回
- export function goBack() {
- router.back();
- }
|