refacetor: download file function
This commit is contained in:
parent
852d8e5801
commit
41fba86f6d
1 changed files with 31 additions and 0 deletions
|
|
@ -1,11 +1,41 @@
|
||||||
<script setup lang="ts">
|
<script setup lang="ts">
|
||||||
|
import axios from 'axios'
|
||||||
import { storeToRefs } from 'pinia'
|
import { storeToRefs } from 'pinia'
|
||||||
|
|
||||||
|
import type { EhrFile } from '@/stores/tree-data'
|
||||||
import { useFileInfoStore } from '@/stores/file-info-data'
|
import { useFileInfoStore } from '@/stores/file-info-data'
|
||||||
|
|
||||||
import FileIcon from '@/components/FileIcon.vue'
|
import FileIcon from '@/components/FileIcon.vue'
|
||||||
|
|
||||||
const { isPreview, fileInfo } = storeToRefs(useFileInfoStore())
|
const { isPreview, fileInfo } = storeToRefs(useFileInfoStore())
|
||||||
const { getType, getFormatDate, getSize } = 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>
|
</script>
|
||||||
|
|
||||||
<template>
|
<template>
|
||||||
|
|
@ -52,6 +82,7 @@ const { getType, getFormatDate, getSize } = useFileInfoStore()
|
||||||
label="ดาวน์โหลด"
|
label="ดาวน์โหลด"
|
||||||
icon="mdi-download"
|
icon="mdi-download"
|
||||||
class="q-py-sm"
|
class="q-py-sm"
|
||||||
|
@click="() => downloadSubmit(fileInfo?.pathname)"
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue