fix:getReport

This commit is contained in:
DESKTOP-1R2VSQH\Lenovo ThinkPad E490 2026-02-09 16:34:20 +07:00
parent 4b40d051e0
commit e6cc3142c5
3 changed files with 19 additions and 16 deletions

View file

@ -10,17 +10,13 @@ async function genReport(data: any, fileName: string, type: string = "docx") {
showLoader();
await axios
.post(config.API.reportTemplate + `/docx`, data, {
headers:
type == "docx"
? {
accept:
"application/vnd.openxmlformats-officedocument.wordprocessingml.document",
"content-Type": "application/json",
}
: {
accept: "application/pdf",
"content-Type": "application/json",
},
headers: {
"content-Type": "application/json",
accept:
type === "docx"
? "application/vnd.openxmlformats-officedocument.wordprocessingml.document"
: "application/pdf",
},
responseType: "arraybuffer",
})
.then((res) => {
@ -34,10 +30,17 @@ async function genReport(data: any, fileName: string, type: string = "docx") {
// สร้าง URL สำหรับไฟล์ Blob
const url = URL.createObjectURL(blob);
// 1. ตัดนามสกุลเดิมที่ติดมากับ fileName ออก (ถ้ามี)
const baseName = fileName.replace(/\.[^/.]+$/, "");
// 2. กำหนดนามสกุลใหม่ตามเงื่อนไข
const extension = type === "docx" ? "docx" : "pdf";
// สร้างลิงก์เพื่อดาวน์โหลดไฟล์
const link = document.createElement("a");
link.href = url;
link.download = `${fileName}.${type === "docx" ? "docx" : "pdf"}`; // กำหนดชื่อไฟล์ที่จะดาวน์โหลด
link.download = `${baseName}.${extension}`;
// link.download = `${fileName}.${type === "docx" ? "docx" : "pdf"}`; // กำหนดชื่อไฟล์ที่จะดาวน์โหลด
document.body.appendChild(link);
link.click();