Skip to content

Commit

Permalink
fix(downloaddata): add headers for fetch in downLoadData (#98)
Browse files Browse the repository at this point in the history
fix #97
  • Loading branch information
LuckyFBB authored Nov 20, 2023
1 parent dd1ffa6 commit 416cfe6
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 4 deletions.
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

0 comments on commit 416cfe6

Please sign in to comment.