refacetor: download file function

This commit is contained in:
puri-ph4tt 2023-11-28 09:24:20 +07:00 committed by Methapon2001
parent 852d8e5801
commit 41fba86f6d
No known key found for this signature in database
GPG key ID: 849924FEF46BD132

View file

@ -1,11 +1,41 @@
<script setup lang="ts">
import axios from 'axios'
import { storeToRefs } from 'pinia'
import type { EhrFile } from '@/stores/tree-data'
import { useFileInfoStore } from '@/stores/file-info-data'
import FileIcon from '@/components/FileIcon.vue'
const { isPreview, 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}`
console.log(formatPath)
const res = await axios.get<EhrFile & { download: string }>(
`${import.meta.env.VITE_API_ENDPOINT}${formatPath}`
)
console.log(res.data.download)
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()
})
}
</script>
<template>
@ -52,6 +82,7 @@ const { getType, getFormatDate, getSize } = useFileInfoStore()
label="ดาวน์โหลด"
icon="mdi-download"
class="q-py-sm"
@click="() => downloadSubmit(fileInfo?.pathname)"
/>
</div>
</div>