umi、post 文件下载
umi + post 下载
- 参考
- 注意点
- responseType: ‘blob’ 必须指定
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
30import React from 'react';
import { Button } from 'antd';
import 'antd/dist/antd.css';
import request from 'umi-request';
export default () => {
function download() {
request('/down', {
method: 'POST',
// 必须加responseType: 'blob',
responseType: 'blob',
}).then(res => {
const blob = new Blob([res]); //注意拿到的是数据流!!
const objectURL = URL.createObjectURL(blob);
let btn = document.createElement('a');
btn.download = '文件.docx'; //文件类型
btn.href = objectURL;
btn.click();
URL.revokeObjectURL(objectURL);
btn = null;
});
}
return (
<>
<Button type="primary" onClick={download}>
下载文件
</Button>
</>
);
};
- responseType: ‘blob’ 必须指定