gaoluyang
2025-03-07 d2a06b827fdcf4b274c503c25b3c21fd727ccbbc
src/plugins/download.js
@@ -4,6 +4,7 @@
import { getToken } from "@/utils/auth";
import errorCode from "@/utils/errorCode";
import { blobValidate } from "@/utils/ruoyi";
import Vue from "vue";
const baseURL = process.env.VUE_APP_BASE_API;
let downloadLoadingInstance;
@@ -80,22 +81,37 @@
        downloadLoadingInstance.close();
      });
  },
  async downloadFileFromUrl(url, filename) {
    try {
      let state = /\.(jpg|jpeg|png|gif)$/i.test(url) // 判断是否为图片
      let url1 = ''
      if (state) {
        url1 = Vue.prototype.javaApi + '/img/' + url;
      } else {
        url1 = Vue.prototype.javaApi + '/word/' + url
      }
      // 使用 fetch 获取文件
      const response = await fetch(url1);
      if (!response.ok) {
        throw new Error('文件下载失败: ' + response.statusText);
      }
      // 将文件转换为 Blob
      const blob = await response.blob();
      // 使用 saveAs 保存文件
      saveAs(blob, filename);
      Message.success("数据导出成功");
    } catch (error) {
      Message.error(error);
    }
  },
  saveAs(text, name, opts) {
    if (typeof text === "string") {
      // 路径下载
    // 流下载
    blobToText(text).then((result) => {
      Message.error(result.msg);
    }).catch(() => {
      saveAs(text, name, opts);
      Message.success("数据导出成功");
    } else {
      // 流下载
      blobToText(text)
        .then((result) => {
          Message.error(result.msg);
        })
        .catch(() => {
          saveAs(text, name, opts);
          Message.success("数据导出成功");
        });
    }
    });
  },
  async printErrMsg(data) {
    const resText = await data.text();