Prechádzať zdrojové kódy

设备导入导出优化

jingyuanchao 1 rok pred
rodič
commit
0941c888fc
1 zmenil súbory, kde vykonal 51 pridanie a 0 odobranie
  1. 51 0
      src/views/system/device/index.vue

+ 51 - 0
src/views/system/device/index.vue

@@ -310,6 +310,7 @@
         :on-progress="handleFileUploadProgress"
         :on-success="handleFileSuccess"
         :auto-upload="false"
+        :before-upload="beforeUpload"
         drag
       >
         <i class="el-icon-upload"></i>
@@ -339,6 +340,7 @@ import {
   listDevice,
   updateDevice,
 } from "@/api/system/device";
+import request from "@/utils/request";
 
 export default {
   /** 引入基础minxins*/
@@ -503,6 +505,55 @@ export default {
       );
       this.getList();
     },
+    beforeUpload(file) {
+      // 返回 false 可以阻止文件上传
+      // 创建 FormData 对象
+
+      let config = {
+        headers: {
+          'Content-Type': 'multipart/form-data'
+        },
+        responseType: 'blob'
+      }
+      const formData = new FormData();
+      // 添加文件到 FormData
+      formData.append("file", file);
+      request.post(this.upload.url, formData, config)
+        .then((response) => {
+          // 在接收到后端响应时执行的逻辑
+          // console.log(response,"response");
+          this.upload.open = false;
+          this.upload.isUploading = false;
+          this.$refs.upload.clearFiles();
+          if (response&&response.size >0) {
+            this.$alert(
+              "导入失败,请根据返回的Excel检查文件内容"
+            );
+            const blob = new Blob([response],
+              {type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'})  //你需要的类型 转化为blob对象
+            const url = window.URL.createObjectURL(blob)         //将对象转化为链接
+            let a = document.createElement('a')
+            // 下载链接
+            a.href = url
+            a.download = '设备导入结果.xlsx'
+            document.body.appendChild(a)
+            // 点击a标签,进行下载
+            a.click()
+            // 移除元素
+            document.body.removeChild(a)
+          } else {
+            this.$alert(
+              "导入成功!"
+            );
+          }
+          this.getList();
+        })
+        .catch((error) => {
+          // 在请求失败时执行的逻辑
+          // console.log(error,"error");
+        });
+      return false;
+    },
     getDefaultOrg(org) {
       this.orgName = org.name;
     },