refactor(registry): downloadBlobFile
This commit is contained in:
parent
9eaa28711d
commit
f07cf25489
10 changed files with 103 additions and 138 deletions
|
|
@ -3,6 +3,7 @@ import { ref, onMounted } from "vue";
|
|||
|
||||
import { useCounterMixin } from "@/stores/mixin";
|
||||
import { useDataStore } from "@/stores/data";
|
||||
import { downloadBlobFile } from "@/modules/10_registry/utils/downloadFile";
|
||||
import http from "@/plugins/http";
|
||||
import config from "@/app.config";
|
||||
|
||||
|
|
@ -137,46 +138,10 @@ async function onDownloadFile(id: string, profileId: string) {
|
|||
)
|
||||
.then(async (res) => {
|
||||
const downloadUrl = res.data.downloadUrl;
|
||||
const response = await fetch(downloadUrl);
|
||||
const blob = await response.blob();
|
||||
|
||||
const contentType: string | null = response.headers.get("Content-Type");
|
||||
|
||||
// 2. สร้างตัวแปลง MIME Type เป็นนามสกุลไฟล์
|
||||
const extensionMap: Record<string, string> = {
|
||||
"application/pdf": "pdf",
|
||||
"image/jpeg": "jpg",
|
||||
"image/png": "png",
|
||||
"image/gif": "gif",
|
||||
"application/zip": "zip",
|
||||
"application/vnd.openxmlformats-officedocument.wordprocessingml.document":
|
||||
"docx",
|
||||
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet":
|
||||
"xlsx",
|
||||
};
|
||||
|
||||
let extension = contentType ? extensionMap[contentType] : undefined;
|
||||
|
||||
if (!extension) {
|
||||
const urlWithoutQuery = downloadUrl.split("?")[0];
|
||||
extension = urlWithoutQuery.includes(".")
|
||||
? urlWithoutQuery.split(".").pop()
|
||||
: "pdf";
|
||||
}
|
||||
|
||||
const blobForDownload = new Blob([blob], {
|
||||
type: "application/octet-stream",
|
||||
await downloadBlobFile({
|
||||
downloadUrl: downloadUrl,
|
||||
fileName: `ประวัติการเปลี่ยนชื่อ-นามสกุล`,
|
||||
});
|
||||
const url = URL.createObjectURL(blobForDownload);
|
||||
const link = document.createElement("a");
|
||||
link.href = url;
|
||||
link.download = `ประวัติการเปลี่ยนชื่อ-นามสกุล.${extension}`;
|
||||
document.body.appendChild(link);
|
||||
link.click();
|
||||
setTimeout(() => {
|
||||
document.body.removeChild(link);
|
||||
URL.revokeObjectURL(url);
|
||||
}, 100);
|
||||
})
|
||||
.catch((err) => {
|
||||
messageError($q, err);
|
||||
|
|
|
|||
|
|
@ -6,6 +6,7 @@ import http from "@/plugins/http";
|
|||
import config from "@/app.config";
|
||||
import { useCounterMixin } from "@/stores/mixin";
|
||||
import { useDataStore } from "@/stores/data";
|
||||
import { downloadBlobFile } from "@/modules/10_registry/utils/downloadFile";
|
||||
|
||||
/** import type */
|
||||
import type { AbilityRows } from "@/modules/10_registry/interface/index/Main";
|
||||
|
|
@ -263,18 +264,10 @@ async function onDownloadFile(id: string, profileId: string) {
|
|||
)
|
||||
.then(async (res) => {
|
||||
const downloadUrl = res.data.downloadUrl;
|
||||
const response = await fetch(downloadUrl);
|
||||
const blob = await response.blob();
|
||||
const url = URL.createObjectURL(blob);
|
||||
const link = document.createElement("a");
|
||||
link.href = url;
|
||||
link.download = `เอกสารความสามารถพิเศษ_`;
|
||||
document.body.appendChild(link);
|
||||
link.click();
|
||||
setTimeout(() => {
|
||||
document.body.removeChild(link);
|
||||
URL.revokeObjectURL(url);
|
||||
}, 100);
|
||||
await downloadBlobFile({
|
||||
downloadUrl: downloadUrl,
|
||||
fileName: `เอกสารความสามารถพิเศษ`,
|
||||
});
|
||||
})
|
||||
.catch((err) => {
|
||||
messageError($q, err);
|
||||
|
|
|
|||
|
|
@ -7,6 +7,7 @@ import config from "@/app.config";
|
|||
import { useCounterMixin } from "@/stores/mixin";
|
||||
import { useDataStore } from "@/stores/data";
|
||||
import { useRegistryDataStore } from "@/modules/10_registry/store/Main";
|
||||
import { downloadBlobFile } from "@/modules/10_registry/utils/downloadFile";
|
||||
|
||||
import type { DisciplineDetail } from "@/modules/10_registry/interface/index/Main";
|
||||
|
||||
|
|
@ -195,19 +196,10 @@ async function onDownloadFile(id: string, profileId: string) {
|
|||
showLoader();
|
||||
try {
|
||||
const res = await getPathUploadFlie(fileGroup.value, profileId, id);
|
||||
const downloadUrl = res.downloadUrl;
|
||||
const response = await fetch(downloadUrl);
|
||||
const blob = await response.blob();
|
||||
const url = URL.createObjectURL(blob);
|
||||
const link = document.createElement("a");
|
||||
link.href = url;
|
||||
link.download = `เอกสารวินัย`;
|
||||
document.body.appendChild(link);
|
||||
link.click();
|
||||
setTimeout(() => {
|
||||
document.body.removeChild(link);
|
||||
URL.revokeObjectURL(url);
|
||||
}, 100);
|
||||
await downloadBlobFile({
|
||||
downloadUrl: res.downloadUrl,
|
||||
fileName: `เอกสารวินัย`,
|
||||
});
|
||||
} catch (e) {
|
||||
messageError($q, e);
|
||||
} finally {
|
||||
|
|
|
|||
|
|
@ -7,6 +7,7 @@ import config from "@/app.config";
|
|||
import { useCounterMixin } from "@/stores/mixin";
|
||||
import { useDataStore } from "@/stores/data";
|
||||
import { useRegistryDataStore } from "@/modules/10_registry/store/Main";
|
||||
import { downloadBlobFile } from "@/modules/10_registry/utils/downloadFile";
|
||||
|
||||
import type { DutyFormType } from "@/modules/10_registry/interface/index/Main";
|
||||
|
||||
|
|
@ -298,19 +299,10 @@ async function onDownloadFile(id: string, profileId: string) {
|
|||
showLoader();
|
||||
try {
|
||||
const res = await getPathUploadFlie(fileGroup.value, profileId, id);
|
||||
const downloadUrl = res.downloadUrl;
|
||||
const response = await fetch(downloadUrl);
|
||||
const blob = await response.blob();
|
||||
const url = URL.createObjectURL(blob);
|
||||
const link = document.createElement("a");
|
||||
link.href = url;
|
||||
link.download = `เอกสารปฏิบัติราชการพิเศษ`;
|
||||
document.body.appendChild(link);
|
||||
link.click();
|
||||
setTimeout(() => {
|
||||
document.body.removeChild(link);
|
||||
URL.revokeObjectURL(url);
|
||||
}, 100);
|
||||
await downloadBlobFile({
|
||||
downloadUrl: res.downloadUrl,
|
||||
fileName: `เอกสารปฏิบัติราชการพิเศษ`,
|
||||
});
|
||||
} catch (e) {
|
||||
messageError($q, e);
|
||||
} finally {
|
||||
|
|
|
|||
|
|
@ -6,6 +6,7 @@ import http from "@/plugins/http";
|
|||
import config from "@/app.config";
|
||||
import { useCounterMixin } from "@/stores/mixin";
|
||||
import { useDataStore } from "@/stores/data";
|
||||
import { downloadBlobFile } from "@/modules/10_registry/utils/downloadFile";
|
||||
|
||||
import type { DutyFormType } from "@/modules/10_registry/interface/index/Main";
|
||||
|
||||
|
|
@ -307,18 +308,10 @@ async function onDownloadFile(id: string, profileId: string) {
|
|||
)
|
||||
.then(async (res) => {
|
||||
const downloadUrl = res.data.downloadUrl;
|
||||
const response = await fetch(downloadUrl);
|
||||
const blob = await response.blob();
|
||||
const url = URL.createObjectURL(blob);
|
||||
const link = document.createElement("a");
|
||||
link.href = url;
|
||||
link.download = `เอกสารช่วยราชการ`;
|
||||
document.body.appendChild(link);
|
||||
link.click();
|
||||
setTimeout(() => {
|
||||
document.body.removeChild(link);
|
||||
URL.revokeObjectURL(url);
|
||||
}, 100);
|
||||
await downloadBlobFile({
|
||||
downloadUrl: downloadUrl,
|
||||
fileName: `เอกสารช่วยราชการ`,
|
||||
});
|
||||
})
|
||||
.catch((err) => {
|
||||
messageError($q, err);
|
||||
|
|
|
|||
|
|
@ -7,6 +7,7 @@ import config from "@/app.config";
|
|||
import { useCounterMixin } from "@/stores/mixin";
|
||||
import { useDataStore } from "@/stores/data";
|
||||
import { useRegistryDataStore } from "@/modules/10_registry/store/Main";
|
||||
import { downloadBlobFile } from "@/modules/10_registry/utils/downloadFile";
|
||||
|
||||
import type { NopaidFormType } from "@/modules/10_registry/interface/index/Main";
|
||||
|
||||
|
|
@ -280,18 +281,10 @@ async function onDownloadFile(id: string, profileId: string) {
|
|||
try {
|
||||
const res = await getPathUploadFlie(fileGroup.value, profileId, id);
|
||||
const downloadUrl = res.downloadUrl;
|
||||
const response = await fetch(downloadUrl);
|
||||
const blob = await response.blob();
|
||||
const url = URL.createObjectURL(blob);
|
||||
const link = document.createElement("a");
|
||||
link.href = url;
|
||||
link.download = `บันทึกวันที่ไม่ได้รับ${salaryText.value}ฯ`;
|
||||
document.body.appendChild(link);
|
||||
link.click();
|
||||
setTimeout(() => {
|
||||
document.body.removeChild(link);
|
||||
URL.revokeObjectURL(url);
|
||||
}, 100);
|
||||
await downloadBlobFile({
|
||||
downloadUrl: downloadUrl,
|
||||
fileName: `บันทึกวันที่ไม่ได้รับ${salaryText.value}ฯ`,
|
||||
});
|
||||
} catch (e) {
|
||||
messageError($q, e);
|
||||
} finally {
|
||||
|
|
|
|||
|
|
@ -7,6 +7,7 @@ import config from "@/app.config";
|
|||
import { useCounterMixin } from "@/stores/mixin";
|
||||
import { useDataStore } from "@/stores/data";
|
||||
import { useRegistryDataStore } from "@/modules/10_registry/store/Main";
|
||||
import { downloadBlobFile } from "@/modules/10_registry/utils/downloadFile";
|
||||
|
||||
import type { CertificateDetail } from "@/modules/10_registry/interface/index/Main";
|
||||
|
||||
|
|
@ -278,19 +279,10 @@ async function onDownloadFile(id: string, profileId: string) {
|
|||
showLoader();
|
||||
try {
|
||||
const data = await getPathUploadFlie(fileGroup.value, profileId, id);
|
||||
const downloadUrl = data.downloadUrl;
|
||||
const response = await fetch(downloadUrl);
|
||||
const blob = await response.blob();
|
||||
const url = URL.createObjectURL(blob);
|
||||
const link = document.createElement("a");
|
||||
link.href = url;
|
||||
link.download = `เอกสารใบอนุญาตประกอบวิชาชีพ`;
|
||||
document.body.appendChild(link);
|
||||
link.click();
|
||||
setTimeout(() => {
|
||||
document.body.removeChild(link);
|
||||
URL.revokeObjectURL(url);
|
||||
}, 100);
|
||||
await downloadBlobFile({
|
||||
downloadUrl: data.downloadUrl,
|
||||
fileName: `เอกสารใบอนุญาตประกอบวิชาชีพ`,
|
||||
});
|
||||
} catch (error) {
|
||||
messageError($q, error);
|
||||
} finally {
|
||||
|
|
|
|||
|
|
@ -7,6 +7,7 @@ import config from "@/app.config";
|
|||
import { useCounterMixin } from "@/stores/mixin";
|
||||
import { useDataStore } from "@/stores/data";
|
||||
import { useRegistryDataStore } from "@/modules/10_registry/store/Main";
|
||||
import { downloadBlobFile } from "@/modules/10_registry/utils/downloadFile";
|
||||
|
||||
import type { InsigniaFormType } from "@/modules/10_registry/interface/index/Main";
|
||||
|
||||
|
|
@ -494,18 +495,10 @@ async function onDownloadFile(id: string, profileId: string) {
|
|||
showLoader();
|
||||
try {
|
||||
const data = await getPathUploadFlie(fileGroup.value, profileId, id);
|
||||
const response = await fetch(data.downloadUrl);
|
||||
const blob = await response.blob();
|
||||
const url = URL.createObjectURL(blob);
|
||||
const link = document.createElement("a");
|
||||
link.href = url;
|
||||
link.download = `เอกสารเครื่องราชอิสริยาภรณ์`;
|
||||
document.body.appendChild(link);
|
||||
link.click();
|
||||
setTimeout(() => {
|
||||
document.body.removeChild(link);
|
||||
URL.revokeObjectURL(url);
|
||||
}, 100);
|
||||
await downloadBlobFile({
|
||||
downloadUrl: data.downloadUrl,
|
||||
fileName: `เอกสารเครื่องราชอิสริยาภรณ์`,
|
||||
});
|
||||
} catch (error) {
|
||||
messageError($q, error);
|
||||
} finally {
|
||||
|
|
|
|||
|
|
@ -7,6 +7,7 @@ import config from "@/app.config";
|
|||
import { useCounterMixin } from "@/stores/mixin";
|
||||
import { useDataStore } from "@/stores/data";
|
||||
import { useRegistryDataStore } from "@/modules/10_registry/store/Main";
|
||||
import { downloadBlobFile } from "@/modules/10_registry/utils/downloadFile";
|
||||
|
||||
import type { HonorFormData } from "@/modules/10_registry/interface/index/Main";
|
||||
|
||||
|
|
@ -278,18 +279,10 @@ async function onDownloadFile(id: string, profileId: string) {
|
|||
showLoader();
|
||||
try {
|
||||
const data = await getPathUploadFlie(fileGroup.value, profileId, id);
|
||||
const response = await fetch(data.downloadUrl);
|
||||
const blob = await response.blob();
|
||||
const url = URL.createObjectURL(blob);
|
||||
const link = document.createElement("a");
|
||||
link.href = url;
|
||||
link.download = `เอกสารประกาศเกียรติคุณ`;
|
||||
document.body.appendChild(link);
|
||||
link.click();
|
||||
setTimeout(() => {
|
||||
document.body.removeChild(link);
|
||||
URL.revokeObjectURL(url);
|
||||
}, 100);
|
||||
await downloadBlobFile({
|
||||
downloadUrl: data.downloadUrl,
|
||||
fileName: `เอกสารประกาศเกียรติคุณ`,
|
||||
});
|
||||
} catch (error) {
|
||||
messageError($q, error);
|
||||
} finally {
|
||||
|
|
|
|||
59
src/modules/10_registry/utils/downloadFile.ts
Normal file
59
src/modules/10_registry/utils/downloadFile.ts
Normal file
|
|
@ -0,0 +1,59 @@
|
|||
export interface DownloadFileOptions {
|
||||
downloadUrl: string;
|
||||
fileName: string;
|
||||
}
|
||||
|
||||
const isMobile =
|
||||
/Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(
|
||||
navigator.userAgent,
|
||||
);
|
||||
|
||||
export async function downloadBlobFile({
|
||||
downloadUrl,
|
||||
fileName,
|
||||
}: DownloadFileOptions): Promise<void> {
|
||||
// Use window.open for desktop, blob download for mobile
|
||||
if (!isMobile) {
|
||||
window.open(downloadUrl, "_blank");
|
||||
return;
|
||||
}
|
||||
|
||||
const response = await fetch(downloadUrl);
|
||||
const blob = await response.blob();
|
||||
|
||||
const contentType: string | null = response.headers.get("Content-Type");
|
||||
|
||||
const extensionMap: Record<string, string> = {
|
||||
"application/pdf": "pdf",
|
||||
"image/jpeg": "jpg",
|
||||
"image/png": "png",
|
||||
"image/gif": "gif",
|
||||
"application/zip": "zip",
|
||||
"application/vnd.openxmlformats-officedocument.wordprocessingml.document":
|
||||
"docx",
|
||||
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet": "xlsx",
|
||||
};
|
||||
|
||||
let extension = contentType ? extensionMap[contentType] : undefined;
|
||||
|
||||
if (!extension) {
|
||||
const urlWithoutQuery = downloadUrl.split("?")[0];
|
||||
extension = urlWithoutQuery.includes(".")
|
||||
? urlWithoutQuery.split(".").pop()
|
||||
: "pdf";
|
||||
}
|
||||
|
||||
const blobForDownload = new Blob([blob], {
|
||||
type: "application/octet-stream",
|
||||
});
|
||||
const url = URL.createObjectURL(blobForDownload);
|
||||
const link = document.createElement("a");
|
||||
link.href = url;
|
||||
link.download = `${fileName}.${extension}`;
|
||||
document.body.appendChild(link);
|
||||
link.click();
|
||||
setTimeout(() => {
|
||||
document.body.removeChild(link);
|
||||
URL.revokeObjectURL(url);
|
||||
}, 100);
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue