26 lines
772 B
TypeScript
26 lines
772 B
TypeScript
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();
|
|
const url = window.URL.createObjectURL(blob);
|
|
|
|
// 다운로드를 트리거할 가짜 <a> 태그 생성
|
|
const link = document.createElement('a');
|
|
link.href = url;
|
|
link.download = fileNm; // 원하는 파일명 지정
|
|
document.body.appendChild(link);
|
|
link.click();
|
|
|
|
// 메모리 해제
|
|
link.remove();
|
|
window.URL.revokeObjectURL(url);
|
|
} catch (error) {
|
|
console.error('다운로드 중 오류 발생:', error);
|
|
}
|
|
}; |