| | |
| | | downloadLoadingInstance.close(); |
| | | }); |
| | | }, |
| | | async saveAs(text, name, opts) { |
| | | if (typeof text === "string") { |
| | | async saveAs(fileUrl, fileName) { |
| | | try { |
| | | let state = /\.(jpg|jpeg|png|gif)$/i.test(text) // 判断是否为图片 |
| | | let url1 = '' |
| | | if (state) { |
| | | url1 = Vue.prototype.javaApi + '/img/' + text; |
| | | } else { |
| | | if (text.startsWith("/word/")) { |
| | | url1 = Vue.prototype.javaApi + text |
| | | } else if (text.startsWith("word/")) { |
| | | url1 = Vue.prototype.javaApi + '/' + text |
| | | } else { |
| | | url1 = Vue.prototype.javaApi + '/word/' + text |
| | | } |
| | | } |
| | | // 使用 fetch 获取文件 |
| | | const response = await fetch(url1); |
| | | if (!response.ok) { |
| | | throw new Error('文件下载失败: ' + response.statusText); |
| | | } |
| | | // 将文件转换为 Blob |
| | | const blob = await response.blob(); |
| | | // 使用 saveAs 保存文件 |
| | | saveAs(blob, name); |
| | | Message.success("数据导出成功"); |
| | | } catch (error) { |
| | | Message.error(error); |
| | | } |
| | | } else { |
| | | // 流下载 |
| | | blobToText(text) |
| | | .then((result) => { |
| | | Message.error(result.msg); |
| | | }) |
| | | .catch(() => { |
| | | saveAs(text, name, opts); |
| | | Message.success("数据导出成功"); |
| | | const response = await axios({ |
| | | method: 'get', |
| | | url: `${Vue.prototype.javaApi}/common/downloadMinio`, |
| | | params: { |
| | | fileUrl, |
| | | fileName |
| | | }, |
| | | responseType: 'blob', |
| | | headers: { Authorization: 'Bearer ' + getToken() } |
| | | }); |
| | | |
| | | if (blobValidate(response.data)) { |
| | | saveAs(new Blob([response.data]), fileName); |
| | | Message.success("下载成功"); |
| | | } else { |
| | | this.printErrMsg(response.data); |
| | | } |
| | | } catch (error) { |
| | | Message.error("下载失败:" + error.message); |
| | | } |
| | | }, |
| | | async printErrMsg(data) { |