forked from langgenius/dify
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdownload.ts
More file actions
34 lines (30 loc) · 900 Bytes
/
download.ts
File metadata and controls
34 lines (30 loc) · 900 Bytes
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
export type DownloadUrlOptions = {
url: string
fileName?: string
rel?: string
target?: string
}
const triggerDownload = ({ url, fileName, rel, target }: DownloadUrlOptions) => {
if (!url)
return
const anchor = document.createElement('a')
anchor.href = url
if (fileName)
anchor.download = fileName
if (rel)
anchor.rel = rel
if (target)
anchor.target = target
anchor.style.display = 'none'
document.body.appendChild(anchor)
anchor.click()
anchor.remove()
}
export const downloadUrl = ({ url, fileName, rel = 'noopener noreferrer', target }: DownloadUrlOptions) => {
triggerDownload({ url, fileName, rel, target })
}
export const downloadBlob = ({ data, fileName }: { data: Blob, fileName: string }) => {
const url = window.URL.createObjectURL(data)
triggerDownload({ url, fileName, rel: 'noopener noreferrer' })
window.URL.revokeObjectURL(url)
}