From fb7087d38a09d13eb2706fe39efc24e75501d075 Mon Sep 17 00:00:00 2001 From: jorgep Date: Tue, 15 Oct 2024 10:26:07 +0200 Subject: [PATCH] fix: refs #7229 download file --- src/composables/downloadFile.js | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/src/composables/downloadFile.js b/src/composables/downloadFile.js index 12639dcd6..f46697fb1 100644 --- a/src/composables/downloadFile.js +++ b/src/composables/downloadFile.js @@ -1,11 +1,28 @@ import { useSession } from 'src/composables/useSession'; import { getUrl } from './getUrl'; +import axios from 'axios'; +import { exportFile } from 'quasar'; const { getTokenMultimedia } = useSession(); const token = getTokenMultimedia(); export async function downloadFile(id, model = 'dms', urlPath = '/downloadFile', url) { - let appUrl = await getUrl('', 'lilium'); - appUrl = appUrl.replace('/#/', ''); - window.open(url ?? `${appUrl}/api/${model}/${id}${urlPath}?access_token=${token}`); + try { + const appUrl = (await getUrl('', 'lilium')).replace('/#/', ''); + const response = await axios.get( + url ?? `${appUrl}/${model}/${id}${urlPath}?access_token=${token}`, + { responseType: 'blob' } + ); + + const contentDisposition = response.headers['content-disposition']; + const matches = /filename[^;=\n]*=((['"]).*?\2|[^;\n]*)/.exec(contentDisposition); + const filename = + matches != null && matches[1] + ? matches[1].replace(/['"]/g, '') + : 'downloaded-file'; + + exportFile(filename, response.data); + } catch (error) { + console.error('Error downloading the file', error); + } }