mirror of
https://github.com/oceanprotocol/ocean.js.git
synced 2024-11-26 20:39:05 +01:00
Fix/ Download file browser (#1758)
* remove head call for download file browser helper method * fix lint
This commit is contained in:
parent
96568ed250
commit
a50cd5bb00
@ -6,14 +6,17 @@ import { DownloadResponse } from '../@types'
|
|||||||
* @param {string} url - The URL of the file to download
|
* @param {string} url - The URL of the file to download
|
||||||
* @returns {Promise<void>} - A Promise that resolves when the file has been downloaded
|
* @returns {Promise<void>} - A Promise that resolves when the file has been downloaded
|
||||||
*/
|
*/
|
||||||
export async function downloadFileBrowser(url: string): Promise<void> {
|
export function downloadFileBrowser(url: string): void {
|
||||||
const headResponse = await fetch(url, { method: 'HEAD' })
|
|
||||||
const contentHeader = headResponse.headers.get('content-disposition')
|
|
||||||
const fileName = contentHeader?.split('=')[1] ? contentHeader?.split('=')[1] : 'file'
|
|
||||||
const xhr = new XMLHttpRequest()
|
const xhr = new XMLHttpRequest()
|
||||||
xhr.responseType = 'blob'
|
xhr.responseType = 'blob'
|
||||||
xhr.open('GET', url)
|
xhr.open('GET', url)
|
||||||
xhr.onload = () => {
|
xhr.onload = () => {
|
||||||
|
const contentDispositionHeader = xhr.getResponseHeader('content-disposition')
|
||||||
|
const fileNameMatch = contentDispositionHeader?.match(
|
||||||
|
/filename[^;=\n]*=((['"]).*?\2|[^;\n]*)/
|
||||||
|
)
|
||||||
|
const fileName = fileNameMatch && fileNameMatch[1] ? fileNameMatch[1] : 'file'
|
||||||
|
|
||||||
const blobURL = window.URL.createObjectURL(xhr.response)
|
const blobURL = window.URL.createObjectURL(xhr.response)
|
||||||
const a = document.createElement('a')
|
const a = document.createElement('a')
|
||||||
a.href = blobURL
|
a.href = blobURL
|
||||||
@ -23,7 +26,7 @@ export async function downloadFileBrowser(url: string): Promise<void> {
|
|||||||
a.remove()
|
a.remove()
|
||||||
window.URL.revokeObjectURL(blobURL)
|
window.URL.revokeObjectURL(blobURL)
|
||||||
}
|
}
|
||||||
xhr.send(null)
|
xhr.send()
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
Loading…
x
Reference in New Issue
Block a user