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

53 lines
1.4 KiB
JavaScript

import { useAxios } from '@/hooks/useAxios'
// 파일 다운로드
export const handleFileDown = async (file) => {
const { promiseGet } = useAxios()
const url = `/api/board/file/download`
const params = new URLSearchParams({
encodeFileNo: file.encodeFileNo,
})
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')
link.href = fileUrl
link.download = file.srcFileNm
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
}