refactor(registry): downloadBlobFile

This commit is contained in:
DESKTOP-1R2VSQH\Lenovo ThinkPad E490 2026-05-08 17:39:01 +07:00
parent 9eaa28711d
commit f07cf25489
10 changed files with 103 additions and 138 deletions

View file

@ -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);