Skip to content

Commit

Permalink
Fix improve file export handling in client
Browse files Browse the repository at this point in the history
  • Loading branch information
minai621 committed Jul 20, 2024
1 parent 19046ed commit 1d6e835
Showing 1 changed file with 12 additions and 3 deletions.
15 changes: 12 additions & 3 deletions frontend/src/hooks/useFileExport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,17 +41,26 @@ export const useFileExport = (): UseFileExportReturn => {
},
{
responseType: "blob",
headers: {
Accept: "application/octet-stream",
},
}
);

const blob = new Blob([response.data], { type: `application/${exportType}` });
const contentDisposition = response.headers["content-disposition"];
const fileNameMatch =
contentDisposition && contentDisposition.match(/filename="?(.+)"?\s*$/i);
const fileName = fileNameMatch ? fileNameMatch[1] : `${documentName}.${exportType}`;

const blob = new Blob([response.data], { type: response.headers["content-type"] });

const url = window.URL.createObjectURL(blob);
const link = document.createElement("a");
link.href = url;
link.setAttribute("download", `${encodeURIComponent(documentName)}.${exportType}`);
link.setAttribute("download", fileName);
document.body.appendChild(link);
link.click();
if (link.parentNode) link.parentNode.removeChild(link);
document.body.removeChild(link);
window.URL.revokeObjectURL(url);

enqueueSnackbar(`${exportType.toUpperCase()} 파일이 성공적으로 내보내졌습니다.`, {
Expand Down

0 comments on commit 1d6e835

Please sign in to comment.