This commit is contained in:
Warunee Tamkoo 2024-11-13 14:37:05 +07:00
parent a201594756
commit 216a3ff8d7
5 changed files with 78 additions and 9 deletions

View file

@ -0,0 +1,49 @@
import axios from "axios";
import config from "@/app.config";
import { useQuasar } from "quasar";
import { useCounterMixin } from "@/stores/mixin";
const $q = useQuasar();
const mixin = useCounterMixin();
const { showLoader, hideLoader, messageError } = mixin;
async function generateTxt(data: any, fileName: string) {
showLoader();
await axios
.post(`${config.API.reportTemplate}/txt`, data, {
headers: {
"Content-Type": "text/plain;charset=utf-8",
},
responseType: "blob",
})
.then((res) => {
const data = res.data;
if (data) {
// สร้าง Blob จาก array buffer
const blob = new Blob([data], { type: "text/plain;charset=utf-8" });
// สร้าง URL สำหรับไฟล์ Blob
const url = URL.createObjectURL(blob);
// สร้างลิงก์เพื่อดาวน์โหลดไฟล์
const link = document.createElement("a");
link.href = url;
link.download = `${fileName}.txt`; // กำหนดชื่อไฟล์ที่จะดาวน์โหลด
document.body.appendChild(link);
link.click();
// ลบ URL ที่สร้างขึ้นหลังจากใช้งาน
URL.revokeObjectURL(url);
}
})
.catch((err) => {
messageError($q, err);
})
.finally(() => {
hideLoader();
});
}
export default generateTxt;