-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.js
52 lines (49 loc) · 1.11 KB
/
index.js
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
/**
* js 文件下载
*
* DownloadHelper.js
* import downloadHelper from './DownloadHelper';
*/
/**
* http文件下载
*
* use:
* downloadHelper.downloadUrl(response.data);
*
* @param url
*/
const downloadUrl = url => {
let iframe = document.createElement('iframe');
iframe.style.display = 'none';
iframe.src = url;
iframe.onload = function () {
document.body.removeChild(iframe)
};
document.body.appendChild(iframe)
};
/**
* 获取字节流文件
*
* use:
* let blob = new Blob([response.data], {type: 'application/vnd.ms-excel'});
* let fileName = '文件名称.csv';
* downloadHelper.downFile(blob, fileName);
*
* @param blob
* @param fileName
*/
function downFile(blob, fileName) {
if (window.navigator.msSaveOrOpenBlob) {
navigator.msSaveBlob(blob, fileName);
} else {
var link = document.createElement('a');
link.href = window.URL.createObjectURL(blob);
link.download = fileName;
link.click();
window.URL.revokeObjectURL(link.href);
}
}
export default {
downloadUrl: downloadUrl,
downFile: downFile
};