|
1 | 1 | import { getToken } from '@/web/support/user/auth'; |
| 2 | +import { hasHttps } from '@fastgpt/web/common/system/utils'; |
2 | 3 |
|
3 | | -export const xmlDownloadFetch = ({ url, filename }: { url: string; filename: string }) => { |
4 | | - const xhr = new XMLHttpRequest(); |
5 | | - xhr.open('GET', url, true); |
6 | | - xhr.setRequestHeader('token', getToken()); |
7 | | - xhr.responseType = 'blob'; |
8 | | - xhr.onload = function (e) { |
9 | | - if (this.status == 200) { |
10 | | - const blob = this.response; |
11 | | - const a = document.createElement('a'); |
12 | | - const url = URL.createObjectURL(blob); |
13 | | - a.href = url; |
14 | | - a.download = filename; |
15 | | - a.click(); |
16 | | - window.URL.revokeObjectURL(url); |
17 | | - } |
18 | | - }; |
19 | | - xhr.send(); |
| 4 | +export const xmlDownloadFetch = async ({ url, filename }: { url: string; filename: string }) => { |
| 5 | + if (hasHttps()) { |
| 6 | + const a = document.createElement('a'); |
| 7 | + a.href = url; |
| 8 | + a.download = filename; |
| 9 | + document.body.appendChild(a); |
| 10 | + a.click(); |
| 11 | + document.body.removeChild(a); |
| 12 | + } else { |
| 13 | + const response = await fetch(url, { |
| 14 | + headers: { |
| 15 | + token: `${getToken()}` |
| 16 | + } |
| 17 | + }); |
| 18 | + if (!response.ok) throw new Error('Network response was not ok.'); |
| 19 | + |
| 20 | + const blob = await response.blob(); |
| 21 | + const downloadUrl = window.URL.createObjectURL(blob); |
| 22 | + const a = document.createElement('a'); |
| 23 | + a.style.display = 'none'; // 隐藏<a>元素 |
| 24 | + a.href = downloadUrl; |
| 25 | + a.download = filename; |
| 26 | + document.body.appendChild(a); |
| 27 | + a.click(); // 模拟用户点击 |
| 28 | + document.body.removeChild(a); |
| 29 | + window.URL.revokeObjectURL(downloadUrl); // 清理生成的URL |
| 30 | + } |
20 | 31 | }; |
0 commit comments