-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
34 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import { CommonAxios } from "../CommonAxios"; | ||
|
||
type TArg = { | ||
fileId: number; | ||
fileName: string; | ||
}; | ||
|
||
// 파일의 ID와 파일명이 있을 때 다운로드 하는 함수 | ||
export async function handleDownloadFile({ fileId, fileName }: TArg) { | ||
const url = await getFileUrlById(fileId); | ||
const a = document.createElement("a"); | ||
|
||
a.href = url; | ||
a.download = fileName; | ||
a.click(); | ||
|
||
URL.revokeObjectURL(url); | ||
} | ||
|
||
// 파일의 ID가 있을 때 파일의 URL을 가져오는 함수 (next Image src 등에서 사용) | ||
export async function getFileUrlById(fileId: number) { | ||
const result = await CommonAxios({ | ||
url: `/files/${fileId}`, | ||
responseType: "blob", | ||
}); | ||
|
||
const blob = new Blob([result.data]); | ||
|
||
const url = URL.createObjectURL(blob); | ||
|
||
return url; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export { handleDownloadFile } from "./handleDownloadFile"; | ||
export { getFileUrlById } from "./handleDownloadFile"; |