fix:test
All checks were successful
Build & Deploy on Dev / build (push) Successful in 1m5s

This commit is contained in:
DESKTOP-1R2VSQH\Lenovo ThinkPad E490 2026-02-09 16:38:40 +07:00
parent e39cce92c3
commit c5de1286d2

View file

@ -20,33 +20,65 @@ async function genReport(data: any, fileName: string, type: string = "docx") {
responseType: "arraybuffer", responseType: "arraybuffer",
}) })
.then((res) => { .then((res) => {
const data = res.data; // const data = res.data;
if (data) { // if (data) {
// สร้าง Blob จาก array buffer // // สร้าง Blob จาก array buffer
const blob = new Blob([data], { // const blob = new Blob([data], {
type: "application/vnd.openxmlformats-officedocument.wordprocessingml.document", // type: "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
}); // });
// สร้าง URL สำหรับไฟล์ Blob // // สร้าง 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 = `${baseName}.${extension}`;
// // link.download = `${fileName}.${type === "docx" ? "docx" : "pdf"}`; // กำหนดชื่อไฟล์ที่จะดาวน์โหลด
// document.body.appendChild(link);
// link.click();
// // ลบ URL ที่สร้างขึ้นหลังจากใช้งาน
// URL.revokeObjectURL(url);
const responseData = res.data;
if (responseData) {
// --- ส่วนที่ปรับปรุง 1: กำหนด MIME Type ให้ตรงกับไฟล์ที่รับมาจริง ---
const mimeType =
type === "docx"
? "application/vnd.openxmlformats-officedocument.wordprocessingml.document"
: "application/pdf";
const blob = new Blob([responseData], { type: mimeType });
const url = URL.createObjectURL(blob); const url = URL.createObjectURL(blob);
// 1. ตัดนามสกุลเดิมที่ติดมากับ fileName ออก (ถ้ามี) // --- ส่วนที่ปรับปรุง 2: จัดการชื่อไฟล์ (ป้องกันนามสกุลซ้อน) ---
const baseName = fileName.replace(/\.[^/.]+$/, ""); // ตัดช่องว่างหัวท้าย และตัดนามสกุลไฟล์เดิมที่อาจติดมาออก (.pdf, .docx ฯลฯ)
const baseName = fileName.trim().replace(/\.[^/.]+$/, "");
// 2. กำหนดนามสกุลใหม่ตามเงื่อนไข
const extension = type === "docx" ? "docx" : "pdf"; const extension = type === "docx" ? "docx" : "pdf";
// สร้างลิงก์เพื่อดาวน์โหลดไฟล์
const link = document.createElement("a"); const link = document.createElement("a");
link.href = url; link.href = url;
link.download = `${baseName}.${extension}`; link.download = `${baseName}.${extension}`;
// link.download = `${fileName}.${type === "docx" ? "docx" : "pdf"}`; // กำหนดชื่อไฟล์ที่จะดาวน์โหลด
// --- ส่วนที่ปรับปรุง 3: สำหรับ Android/Mobile ---
// บางครั้งการ click() ทันทีอาจถูก block หรือมีปัญหา ต้อง append เข้าไปจริงๆ
document.body.appendChild(link); document.body.appendChild(link);
link.click(); link.click();
// ลบ URL ที่สร้างขึ้นหลังจากใช้งาน // หน่วงเวลาเล็กน้อยก่อนลบ element และ revoke URL เพื่อให้ Browser ทำงานเสร็จ
URL.revokeObjectURL(url); setTimeout(() => {
document.body.removeChild(link);
URL.revokeObjectURL(url);
}, 100);
} }
// }
}) })
.catch((err) => { .catch((err) => {
console.log(err); console.log(err);