1
0
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:
Bogdan Fazakas 2023-08-01 20:23:36 +03:00 committed by GitHub
parent 96568ed250
commit a50cd5bb00
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -6,14 +6,17 @@ import { DownloadResponse } from '../@types'
* @param {string} url - The URL of the file to download
* @returns {Promise<void>} - A Promise that resolves when the file has been downloaded
*/
export async function downloadFileBrowser(url: string): Promise<void> {
const headResponse = await fetch(url, { method: 'HEAD' })
const contentHeader = headResponse.headers.get('content-disposition')
const fileName = contentHeader?.split('=')[1] ? contentHeader?.split('=')[1] : 'file'
export function downloadFileBrowser(url: string): void {
const xhr = new XMLHttpRequest()
xhr.responseType = 'blob'
xhr.open('GET', url)
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 a = document.createElement('a')
a.href = blobURL
@ -23,7 +26,7 @@ export async function downloadFileBrowser(url: string): Promise<void> {
a.remove()
window.URL.revokeObjectURL(blobURL)
}
xhr.send(null)
xhr.send()
}
/**