mirror of
https://github.com/oceanprotocol/ocean.js.git
synced 2024-11-26 20:39:05 +01:00
fix download (#1563)
This commit is contained in:
parent
bbab53c7bf
commit
99936ecc59
@ -13,10 +13,23 @@ export async function fetchData(url: string, opts: RequestInit): Promise<Respons
|
|||||||
}
|
}
|
||||||
|
|
||||||
export async function downloadFileBrowser(url: string): Promise<void> {
|
export async function downloadFileBrowser(url: string): Promise<void> {
|
||||||
const anchor = document.createElement('a')
|
const headResponse = await fetch(url, { method: 'HEAD' })
|
||||||
anchor.download = ''
|
const contentHeader = headResponse.headers.get('content-disposition')
|
||||||
anchor.href = url
|
const fileName = contentHeader.split('=')[1]
|
||||||
anchor.click()
|
const xhr = new XMLHttpRequest()
|
||||||
|
xhr.responseType = 'blob'
|
||||||
|
xhr.open('GET', url)
|
||||||
|
xhr.onload = () => {
|
||||||
|
const blobURL = window.URL.createObjectURL(xhr.response)
|
||||||
|
const a = document.createElement('a')
|
||||||
|
a.href = blobURL
|
||||||
|
a.setAttribute('download', fileName)
|
||||||
|
document.body.appendChild(a)
|
||||||
|
a.click()
|
||||||
|
a.remove()
|
||||||
|
window.URL.revokeObjectURL(blobURL)
|
||||||
|
}
|
||||||
|
xhr.send(null)
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function downloadFile(
|
export async function downloadFile(
|
||||||
|
Loading…
x
Reference in New Issue
Block a user