fix file .txt

This commit is contained in:
DESKTOP-1R2VSQH\Lenovo ThinkPad E490 2024-11-22 11:07:30 +07:00
parent 8805627d43
commit 36e21c2e4e
3 changed files with 33 additions and 31 deletions

View file

@ -31,7 +31,7 @@ defineProps<{
<q-separator /> <q-separator />
<q-card-actions align="right"> <q-card-actions align="right">
<q-btn <q-btn
label="ดาวน์โหลด (.docx)" label="ดาวน์โหลด (.TXT)"
color="blue" color="blue"
type="submit" type="submit"
icon="download" icon="download"

View file

@ -28,7 +28,7 @@ import generateTxt from "@/plugins/generateTxt";
const $q = useQuasar(); const $q = useQuasar();
const storeData = useDataStore(); const storeData = useDataStore();
const { logData, size, searchAfter, systemName, date } = storeToRefs(storeData); const { logData, size, searchAfter, systemName, date } = storeToRefs(storeData);
const { date2Thai, messageError, hideLoader, dateToISO } = useCounterMixin(); const { date2Thai, messageError, hideLoader, showLoader } = useCounterMixin();
const startTime = ref<Date | null>(null); // const startTime = ref<Date | null>(null); //
const endTime = ref<Date | null>(null); // const endTime = ref<Date | null>(null); //
@ -463,21 +463,12 @@ function onSendCSV() {
} }
async function downloadTxt() { async function downloadTxt() {
const queryString = { if (currentlogData.value?.id) {
id: currentlogData.value?.id, await generateTxt(
}; currentlogData.value?.id,
await http `LOG_${date2Thai(new Date(startDate.value))}`
.get(`${config.API.log}/report/logsDetail`, { );
params: queryString, }
})
.then(async (res) => {
const data = res.data;
await generateTxt(data, `LOG_${date2Thai(new Date(startDate.value))}`);
})
.catch((e) => {
messageError($q, e);
})
.finally(() => {});
} }
onBeforeMount(async () => { onBeforeMount(async () => {

View file

@ -1,32 +1,32 @@
import axios from "axios";
import config from "@/app.config"; import config from "@/app.config";
import { useQuasar } from "quasar"; import { useQuasar } from "quasar";
import { useCounterMixin } from "@/stores/mixin"; import { useCounterMixin } from "@/stores/mixin";
import http from "@/plugins/http";
const $q = useQuasar(); const $q = useQuasar();
const mixin = useCounterMixin(); const mixin = useCounterMixin();
const { showLoader, hideLoader, messageError } = mixin; const { showLoader, hideLoader, messageError } = mixin;
async function generateTxt(data: any, fileName: string) { async function generateTxt(id: string, fileName: string) {
showLoader(); showLoader();
await axios await http
.post(`${config.API.reportTemplate}/docx`, data, { .post(`${config.API.log}/report/logsDetail?id=${id}`, "", {
headers: { headers: {
accept: "Content-Type": "text/plain;charset=utf-8",
"application/vnd.openxmlformats-officedocument.wordprocessingml.document",
"content-Type": "application/json",
// "Content-Type": "text/plain;charset=utf-8",
}, },
// responseType: "blob", responseType: "blob",
responseType: "arraybuffer",
}) })
.then((res) => { .then(async (res) => {
const data = res.data; const data = await res.data.text();
if (data) { if (data) {
const formattedData = formatData(data);
// สร้าง Blob จาก array buffer // สร้าง Blob จาก array buffer
const blob = new Blob([data], { type: "text/plain;charset=utf-8" }); const blob = new Blob([formattedData], {
type: "text/plain;charset=utf-8",
});
// สร้าง URL สำหรับไฟล์ Blob // สร้าง URL สำหรับไฟล์ Blob
const url = URL.createObjectURL(blob); const url = URL.createObjectURL(blob);
@ -34,7 +34,7 @@ async function generateTxt(data: any, fileName: string) {
// สร้างลิงก์เพื่อดาวน์โหลดไฟล์ // สร้างลิงก์เพื่อดาวน์โหลดไฟล์
const link = document.createElement("a"); const link = document.createElement("a");
link.href = url; link.href = url;
link.download = `${fileName}.docx`; // กำหนดชื่อไฟล์ที่จะดาวน์โหลด link.download = `${fileName}.txt`; // กำหนดชื่อไฟล์ที่จะดาวน์โหลด
document.body.appendChild(link); document.body.appendChild(link);
link.click(); link.click();
@ -50,4 +50,15 @@ async function generateTxt(data: any, fileName: string) {
}); });
} }
function formatData(data: any) {
// ตรวจสอบว่าเป็น JSON และจัดรูปแบบ
try {
const jsonData = JSON.parse(data); // แปลงข้อมูลเป็น JSON
return JSON.stringify(jsonData, null, 2); // จัดให้สวยงาม (2 ช่องว่าง)
} catch {
// ถ้าไม่ใช่ JSON ให้คืนค่าข้อมูลดั้งเดิม
return data;
}
}
export default generateTxt; export default generateTxt;