Front-end/JavaScript
[파일다운로드 두가지 방법] javascript로 다운로드 / buffter 받아서 다운로드
꼬바리
2023. 1. 17. 14:21
javascript 내 코드로 다운로드 가능한 코드
export const downLoad = (fileNm,contents) => {
let fileDown ="data:json;charset=utf-8," + contents;
let encodedUri = encodeURI(fileDown);
let link = document.createElement("a");
link.setAttribute("href", encodedUri);
link.setAttribute("download", fileNm+'.json');
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
}
서버로 buffer받아서 다운로드 진행
axios.post(`/api` , params)
.then((res) => {
let date = new Date
let fileNm = moment(date).format('YYYYMMDDhhmmss')
let bytes = new Uint8Array(res.data.singleBuffer.data);
let blob = new Blob([bytes]);
let link = document.createElement('a');
link.href = window.URL.createObjectURL(blob);
link.download = fileNm+'.json'
link.click();
link.remove();
})
728x90
반응형