|
@@ -0,0 +1,27 @@
|
|
|
+import { httpApp } from "@/plugins/axiosApp";
|
|
|
+
|
|
|
+// 下载文件
|
|
|
+export async function downloadFileURL(url: string, name?: string) {
|
|
|
+ return httpApp
|
|
|
+ .get(url, { responseType: "blob" })
|
|
|
+ .then((res) => {
|
|
|
+ const { data, headers } = res;
|
|
|
+ const fileName = headers["content-disposition"].replace(
|
|
|
+ /\w+;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);
|
|
|
+ })
|
|
|
+ .catch((err) => {});
|
|
|
+}
|