qcast-front/src/util/board-utils.js

63 lines
1.7 KiB
JavaScript

import { useAxios } from '@/hooks/useAxios'
// 파일 다운로드
export const handleFileDown = async (keyNo, zipYn) => {
const { promiseGet } = useAxios()
const url = `/api/board/file/download`
const params = new URLSearchParams({
keyNo: keyNo,
zipYn: zipYn,
})
const options = { responseType: 'blob' }
const apiUrl = `${url}?${params.toString()}`
await promiseGet({ url: apiUrl, option: options })
.then((resultData) => {
if (resultData) {
const blob = new Blob([resultData.data], { type: resultData.headers['content-type'] || 'application/octet-stream' })
const fileUrl = window.URL.createObjectURL(blob)
const link = document.createElement('a')
const contentDisposition = resultData.headers.get('content-disposition')
let filename = 'filename'
if (contentDisposition) {
const matches = contentDisposition.match(/filename="?([^"]+)"?/)
if (matches && matches[1]) {
filename = matches[1]
}
}
link.download = decodeURIComponent(filename)
link.href = fileUrl
document.body.appendChild(link)
link.click()
link.remove()
window.URL.revokeObjectURL(fileUrl)
}
})
.catch((error) => {
alert('File does not exist.')
})
}
// 페이지 번호 생성
export const generateBlockPagination = (currentPage, totalPages, pageBlock) => {
const currentBlock = Math.ceil(currentPage / pageBlock)
let startPage = (currentBlock - 1) * pageBlock + 1
let endPage = startPage + pageBlock - 1
if (endPage > totalPages) {
endPage = totalPages
}
let pageArr = []
for (let i = startPage; i <= endPage; i++) {
pageArr.push(i)
}
return pageArr
}