Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion docs/utils.md
Original file line number Diff line number Diff line change
Expand Up @@ -342,13 +342,16 @@ Utils.removeEmpty({ a: 'test', b: undefined, c: { d: undefined } }) // { a: 'tes
如果不传文件名fileName,会从`response`的`Content-disposition`读取

```js
const headers = new Header();
headers.set('X-Project-ID', 111);
downLoadData({
url: 'downloadUrl',
payload: { a: 1, b: 2 },
headers,
fileName: 'test.pdf',
successCallback: () => message.info('下载成功'),
errorCallback: () => message.error('下载失败'),
finallyCallback: () => this.setState({ visiable: false })
finallyCallback: () => this.setState({ visible: false })
})
```

Expand Down
7 changes: 4 additions & 3 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,11 @@ interface BrowserInter {
safari?: string;
opera?: string;
}
interface DownloadParams {
export interface DownloadParams {
url: string;
fileName?: string;
payload?: object;
headers?: HeadersInit;
finallyCallback?: () => void;
successCallback?: (res: Response) => void;
errorCallback: (res: Response) => void;
Expand Down Expand Up @@ -471,8 +472,8 @@ const utils = {
* @param {function} finallyCallback 成功/失败都会执行回调函数,例如控制一些visible显示隐藏
* */
downLoadData(params: DownloadParams) {
const { url, payload, finallyCallback, successCallback, errorCallback } = params;
fetch(url, { method: 'POST', body: JSON.stringify(payload) })
const { url, payload, headers, finallyCallback, successCallback, errorCallback } = params;
fetch(url, { method: 'POST', body: JSON.stringify(payload), headers })
.then((response) => {
if (response.status !== 200) return errorCallback(response);
const contentType = response.headers.get('Content-type') || '';
Expand Down