Skip to content

Commit cef017b

Browse files
committed
feat: add util for default file name
1 parent d6f12d6 commit cef017b

File tree

2 files changed

+9
-9
lines changed

2 files changed

+9
-9
lines changed

src/Shared/Hooks/UseDownload/UseDownload.tsx

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { API_STATUS_CODES } from '@Common/Constants'
66
import { ServerErrors } from '@Common/ServerError'
77
import { getDownloadResponse } from './service'
88
import { HandleDownloadProps } from './types'
9+
import { getDefaultFileName } from './utils'
910

1011
const useDownload = () => {
1112
const [isDownloading, setIsDownloading] = useState<boolean>(false)
@@ -46,15 +47,7 @@ const useDownload = () => {
4647
const a = document.createElement('a')
4748
a.href = blobUrl
4849

49-
a.download =
50-
fileName ||
51-
response.headers // File name from response headers
52-
?.get('content-disposition')
53-
?.split(';')
54-
?.find((n) => n.includes('filename='))
55-
?.replace('filename=', '')
56-
.trim() ||
57-
'file.tgz'
50+
a.download = fileName || getDefaultFileName(response.headers) || 'file.tgz'
5851

5952
// Append the link element to the DOM
6053
document.body.appendChild(a)
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
export const getDefaultFileName = (headers: Headers) =>
2+
headers
3+
?.get('content-disposition')
4+
?.split(';')
5+
?.find((n) => n.includes('filename='))
6+
?.replace('filename=', '')
7+
.trim()

0 commit comments

Comments
 (0)