ny
昨天 282fbc6488f4e8ceb5fda759f963ee88fbf7b999
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
import type { TargetContext } from '@vben/types/global';
 
import { useAppConfig } from '@vben/hooks';
 
import { openWindow, urlToBase64 } from '..';
import { dataURLtoBlob } from './base64Conver';
 
const { apiURL, reportApiURL } = useAppConfig(
  (import.meta as any).env,
  (import.meta as any).env.PROD,
);
 
/**
 * Download online pictures
 * @param url
 * @param filename
 * @param mime
 * @param bom
 */
export function downloadByOnlineUrl(
  url: string,
  filename: string,
  mime?: string,
  bom?: BlobPart,
) {
  urlToBase64(url).then((base64) => {
    downloadByBase64(base64, filename, mime, bom);
  });
}
 
/**
 * Download pictures based on base64
 * @param buf
 * @param filename
 * @param mime
 * @param bom
 */
export function downloadByBase64(
  buf: string,
  filename: string,
  mime?: string,
  bom?: BlobPart,
) {
  const base64Buf = dataURLtoBlob(buf);
  downloadByData(base64Buf, filename, mime, bom);
}
 
/**
 * Download according to the background interface file stream
 * @param {*} data
 * @param {*} filename
 * @param {*} mime
 * @param {*} bom
 */
export function downloadByData(
  data: BlobPart,
  filename: string,
  mime?: string,
  bom?: BlobPart,
) {
  const blobData = bom === undefined ? [data] : [bom, data];
  const blob = new Blob(blobData, { type: mime || 'application/octet-stream' });
 
  const blobURL = window.URL.createObjectURL(blob);
  const tempLink = document.createElement('a');
  tempLink.style.display = 'none';
  tempLink.href = blobURL;
  tempLink.setAttribute('download', filename);
  if (tempLink.download === undefined) {
    tempLink.setAttribute('target', '_blank');
  }
  document.body.append(tempLink);
  tempLink.click();
  tempLink.remove();
  window.URL.revokeObjectURL(blobURL);
}
 
/**
 * Download file according to file address
 * @param {*} sUrl
 */
export function downloadByUrl({
  fileName,
  target = '_blank',
  url,
}: {
  fileName?: string;
  target?: TargetContext;
  url: string;
}): boolean {
  if (!url) return false;
 
  const isChrome = window.navigator.userAgent.toLowerCase().includes('chrome');
  const isSafari = window.navigator.userAgent.toLowerCase().includes('safari');
  const isFirefox = window.navigator.userAgent
    .toLowerCase()
    .includes('firefox');
 
  if (!/https?:\/\//.test(url) && !url.includes('data:image/png;base64'))
    url = apiURL + url;
  if (
    fileName &&
    !url.includes('data:image/png;base64') &&
    !url.includes('&name=') &&
    !url.includes('?name=')
  ) {
    url =
      url +
      (url.includes('?')
        ? `&name=${encodeURIComponent(fileName)}`
        : `?name=${encodeURIComponent(fileName)}`);
  }
  if (/iP/.test(window.navigator.userAgent)) {
    console.error('Your browser does not support download!');
    return false;
  }
  if (isChrome || isSafari || isFirefox) {
    const link = document.createElement('a');
    link.href = url;
    link.download = fileName || '';
    if (document.createEvent) {
      const e = document.createEvent('MouseEvents');
      e.initEvent('click', true, true);
      link.dispatchEvent(e);
      return true;
    }
  }
  if (!url.includes('?')) {
    url += '?download';
  }
  openWindow(url, { target });
  return true;
}
 
export function downloadByUrlReport({
  fileName,
  target = '_blank',
  url,
}: {
  fileName?: string;
  target?: TargetContext;
  url: string;
}): boolean {
  if (!url) return false;
  const isChrome = window.navigator.userAgent.toLowerCase().includes('chrome');
  const isSafari = window.navigator.userAgent.toLowerCase().includes('safari');
  const isFirefox = window.navigator.userAgent
    .toLowerCase()
    .includes('firefox');
 
  if (!/https?:\/\//.test(url) && !url.includes('data:image/png;base64'))
    url = reportApiURL + url;
  if (
    fileName &&
    !url.includes('data:image/png;base64') &&
    !url.includes('&name=') &&
    !url.includes('?name=')
  ) {
    url =
      url +
      (url.includes('?')
        ? `&name=${encodeURIComponent(fileName)}`
        : `?name=${encodeURIComponent(fileName)}`);
  }
  if (/iP/.test(window.navigator.userAgent)) {
    console.error('Your browser does not support download!');
    return false;
  }
  if (isChrome || isSafari || isFirefox) {
    const link = document.createElement('a');
    link.href = url;
    link.download = fileName || '';
    if (document.createEvent) {
      const e = document.createEvent('MouseEvents');
      e.initEvent('click', true, true);
      link.dispatchEvent(e);
      return true;
    }
  }
  if (!url.includes('?')) {
    url += '?download';
  }
  openWindow(url, { target });
  return true;
}