From 1a3c179f20502ed0ab44fe44fad6462159536708 Mon Sep 17 00:00:00 2001 From: puri-ph4tt Date: Wed, 6 Dec 2023 10:16:23 +0700 Subject: [PATCH] fix: download in subfolder --- .../01_user/components/FileDownload.vue | 52 +++++++++++-------- 1 file changed, 31 insertions(+), 21 deletions(-) diff --git a/Services/client/src/modules/01_user/components/FileDownload.vue b/Services/client/src/modules/01_user/components/FileDownload.vue index ac97661..1ad279f 100644 --- a/Services/client/src/modules/01_user/components/FileDownload.vue +++ b/Services/client/src/modules/01_user/components/FileDownload.vue @@ -11,27 +11,37 @@ import FileIcon from '@/components/FileIcon.vue' const { isFilePreview, fileInfo } = storeToRefs(useFileInfoStore()) const { getType, getFormatDate, getSize } = useFileInfoStore() -async function downloadSubmit(path: any) { - const [cabinet, drawer, folder, file] = path.split('/') - const formatPath = `/cabinet/${cabinet}/drawer/${drawer}/folder/${folder}/file/${file}` - const res = await axiosClient.get( - `${import.meta.env.VITE_API_ENDPOINT}${formatPath}`, - ) - await axios - .get(res.data.download, { - method: 'GET', - responseType: 'blob', - headers: { - 'Content-Type': 'application/json', - Accept: res.data.fileType, - }, - }) - .then((r) => { - const a = document.createElement('a') - a.href = window.URL.createObjectURL(r.data) - a.download = res.data.fileName - a.click() - }) +async function downloadSubmit(path: string | undefined) { + if (path) { + let formatPath: string + + if (path.split('/').length - 1 === 3) { + const [cabinet, drawer, folder, file] = path.split('/') + formatPath = `cabinet/${cabinet}/drawer/${drawer}/folder/${folder}/file/${file}` + } else { + const [cabinet, drawer, folder, subfolder, file] = path.split('/') + formatPath = `cabinet/${cabinet}/drawer/${drawer}/folder/${folder}/subfolder/${subfolder}/file/${file}` + } + + const res = await axiosClient.get( + `${import.meta.env.VITE_API_ENDPOINT}${formatPath}`, + ) + await axios + .get(res.data.download, { + method: 'GET', + responseType: 'blob', + headers: { + 'Content-Type': 'application/json', + Accept: res.data.fileType, + }, + }) + .then((r) => { + const a = document.createElement('a') + a.href = window.URL.createObjectURL(r.data) + a.download = res.data.fileName + a.click() + }) + } }