fix:getReport
This commit is contained in:
parent
4b40d051e0
commit
e6cc3142c5
3 changed files with 19 additions and 16 deletions
|
|
@ -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();
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue