Browse Source

下载模板

Michael Wang 3 năm trước cách đây
mục cha
commit
ac7efbdad4

+ 8 - 0
src/features/subOrg/SubOrg.vue

@@ -113,6 +113,9 @@
         <a-form-item label="文件地址">
           <input id="file-input" :multiple="false" type="file" />
         </a-form-item>
+        <a-form-item label="下载模板">
+          <a-button @click="downloadTpl">下载模板</a-button>
+        </a-form-item>
       </a-form>
     </a-modal>
   </div>
@@ -128,6 +131,7 @@ import {
   updateSubOrg,
 } from "@/api/subOrgPage";
 import { useMainStore } from "@/store";
+import { downloadFileURL } from "@/utils/utils";
 import { message, Modal } from "ant-design-vue";
 import { watch, onMounted, ref, reactive, toRaw } from "vue-demi";
 
@@ -304,4 +308,8 @@ const rowSelection = {
     selectIds = selectedRowKeys as number[];
   },
 };
+
+async function downloadTpl() {
+  downloadFileURL("/api/ess/org/template");
+}
 </script>

+ 8 - 0
src/features/userManagement/UserManagement.vue

@@ -134,6 +134,9 @@
         <a-form-item label="文件地址">
           <input id="file-input" :multiple="false" type="file" />
         </a-form-item>
+        <a-form-item label="下载模板">
+          <a-button @click="downloadTpl">下载模板</a-button>
+        </a-form-item>
       </a-form>
     </a-modal>
   </div>
@@ -149,6 +152,7 @@ import {
   updateUser,
 } from "@/api/userManagementPage";
 import { useMainStore } from "@/store";
+import { downloadFileURL } from "@/utils/utils";
 import { message, Modal } from "ant-design-vue";
 import { watch, onMounted, ref, reactive, toRaw } from "vue-demi";
 
@@ -329,4 +333,8 @@ const rowSelection = {
     selectIds = selectedRowKeys as number[];
   },
 };
+
+async function downloadTpl() {
+  downloadFileURL("/api/ess/org/template");
+}
 </script>

+ 27 - 0
src/utils/utils.ts

@@ -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) => {});
+}