fix:genReport
This commit is contained in:
parent
8c6751ee55
commit
021b12ea8d
1 changed files with 18 additions and 11 deletions
|
|
@ -29,25 +29,32 @@ async function genReport(
|
||||||
responseType: "arraybuffer",
|
responseType: "arraybuffer",
|
||||||
})
|
})
|
||||||
.then((res) => {
|
.then((res) => {
|
||||||
const data = res.data;
|
const responseData = res.data;
|
||||||
if (data) {
|
if (responseData) {
|
||||||
// สร้าง Blob จาก array buffer
|
// --- ส่วนที่ปรับปรุง 1: กำหนด MIME Type ให้ตรงกับไฟล์ที่รับมาจริง ---
|
||||||
const blob = new Blob([data], {
|
const mimeType =
|
||||||
type: "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
|
type === "docx"
|
||||||
});
|
? "application/vnd.openxmlformats-officedocument.wordprocessingml.document"
|
||||||
|
: "application/pdf";
|
||||||
|
|
||||||
// สร้าง URL สำหรับไฟล์ Blob
|
const blob = new Blob([responseData], { type: mimeType });
|
||||||
const url = URL.createObjectURL(blob);
|
const url = URL.createObjectURL(blob);
|
||||||
|
|
||||||
// สร้างลิงก์เพื่อดาวน์โหลดไฟล์
|
// --- ส่วนที่ปรับปรุง 2: จัดการชื่อไฟล์ (ป้องกันนามสกุลซ้อน) ---
|
||||||
|
const baseName = fileName.trim();
|
||||||
|
const extension = type === "docx" ? "docx" : "pdf";
|
||||||
|
|
||||||
const link = document.createElement("a");
|
const link = document.createElement("a");
|
||||||
link.href = url;
|
link.href = url;
|
||||||
link.download = `${fileName}.${type === "docx" ? "docx" : "pdf"}`; // กำหนดชื่อไฟล์ที่จะดาวน์โหลด
|
link.download = `${baseName}.${extension}`;
|
||||||
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) => {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue