26 lines
777 B
TypeScript
26 lines
777 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();
|
|
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);
|
|
} |