From 99936ecc593771eb75ae3ac902c9f03e20617ae1 Mon Sep 17 00:00:00 2001 From: mihaisc Date: Thu, 21 Jul 2022 03:47:09 -0700 Subject: [PATCH] fix download (#1563) --- src/utils/FetchHelper.ts | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/src/utils/FetchHelper.ts b/src/utils/FetchHelper.ts index 0fdc5eb5..338565e1 100644 --- a/src/utils/FetchHelper.ts +++ b/src/utils/FetchHelper.ts @@ -13,10 +13,23 @@ export async function fetchData(url: string, opts: RequestInit): Promise { - const anchor = document.createElement('a') - anchor.download = '' - anchor.href = url - anchor.click() + const headResponse = await fetch(url, { method: 'HEAD' }) + const contentHeader = headResponse.headers.get('content-disposition') + const fileName = contentHeader.split('=')[1] + 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(