# 上传下载
const openDownloadDialog = (url, fileName) => {
if (typeof url === 'object' && url instanceof Blob) {
url = URL.createObjectURL(url); // 创建blob地址
}
const aLink = document.createElement('a');
aLink.href = url;
aLink.download = fileName;
aLink.click();
};
export default {
/**
* 保存CSV文件
* @params content csv文件内容
* @params fileName 保存的文件名
*/
saveTXT: (content, fileName) => {
const blob = new Blob(['\ufeff' + content], {
type: 'text/tet,charset=UTF-8'
});
openDownloadDialog(blob, `${fileName}.txt`);
}
};
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
import axios from 'axios';
import cookie from './cookie';
function _download(content, fileName) {
const blob = new Blob([content]);
const url = window.URL.createObjectURL(blob);
const aLink = document.createElement('a');
aLink.style.display = 'none';
aLink.href = url;
aLink.setAttribute('download', fileName);
document.body.appendChild(aLink);
aLink.click();
}
export default function download(url, fileName, options) {
const defaultOptions = {
credentials: 'include'
};
const newOptions = {
...defaultOptions,
...options
};
const headers = newOptions.headers || {};
newOptions.headers = {
...headers
};
newOptions.url = url;
const token = cookie.get('token');
if (token) {
newOptions.headers.Authorization = `GRJWT ${token}`;
}
newOptions.responseType = 'blob';
newOptions.method = 'get';
axios(newOptions).then(res => {
const reader = new FileReader();
const { data } = res;
reader.onload = () => {
// if (!fileName) {
// const contentDisposition = res.headers['content-disposition'];
// if (contentDisposition) {
// fileName = window.decodeURI(
// res.headers['content-disposition'].split('=')[2].split("''")[1],
// 'UTF-8'
// );
// }
// }
_download(data, fileName);
};
reader.readAsText(data);
});
}
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
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