hrms-mgt/src/plugins/genreportxlsx.ts
DESKTOP-1R2VSQH\Lenovo ThinkPad E490 faf9c4c7f3 fix Vue warn
2025-06-05 15:11:23 +07:00

60 lines
1.7 KiB
TypeScript

import axios from "axios";
import config from "@/app.config";
import { useCounterMixin } from "@/stores/mixin";
const mixin = useCounterMixin();
const { showLoader, hideLoader } = mixin;
async function genReportXLSX(
data: any,
fileName: string,
type: string = "xlsx",
folder: string = ""
) {
showLoader();
await axios
.post(`${config.API.reportTemplate}/xlsx${folder}`, data, {
headers:
type == "xlsx"
? {
accept:
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
"content-Type": "application/json",
}
: {
accept: "application/pdf",
"content-Type": "application/json",
},
responseType: "blob",
})
.then((res) => {
const data = res.data;
if (data) {
// สร้าง Blob จาก array buffer
const blob = new Blob([data]);
// สร้าง URL สำหรับไฟล์ Blob
const url = URL.createObjectURL(blob);
// สร้างลิงก์เพื่อดาวน์โหลดไฟล์
const link = document.createElement("a");
link.href = url;
link.download = `${fileName}.${type}`; // กำหนดชื่อไฟล์ที่จะดาวน์โหลด
document.body.appendChild(link);
link.click();
// ลบ URL ที่สร้างขึ้นหลังจากใช้งาน
URL.revokeObjectURL(url);
}
})
.catch((e) => {
console.log(e);
})
.finally(() => {
hideLoader();
});
}
export default genReportXLSX;