Files
frontend/apps/sn/app/components/lib/utils/fileDownload.ts
T
swcho d898121764 refactor, add:
- searchModal Store 패턴을 사용하여 상태 관리 및 UI, 비즈니스로직 분리
- tsconfig.json에 import paths 추가
2025-09-08 14:49:23 +09:00

27 lines
840 B
TypeScript

const BASE_URL = process.env.NEXT_PUBLIC_SERVER_API_URL || '';
export const handleDownload = async (api: string, fileNm: string) => {
try {
const response = await fetch(api, {
method: 'GET',
});
if (!response.ok) throw new Error('파일 다운로드 실패');
const blob = await response.blob();
fileDownload(blob, fileNm);
} catch (error) {
console.error('다운로드 중 오류 발생:', error);
}
};
export const fileDownload = (data: Blob, fileName: string) => { // Blob데이터 임시link로 연결
const url = window.URL.createObjectURL(data);
const link = document.createElement('a');
link.href = url;
link.download = fileName; // 원하는 파일명 지정
document.body.appendChild(link);
link.click();
// 메모리 해제
link.remove();
window.URL.revokeObjectURL(url);
}