[공부용]참고 사이트 모음/[자바스크립트]
ajax 로 파일 다운로드 코드 예시
bled
2021. 5. 24. 17:47
https://codepen.io/chrisdpratt/pen/RKxJNo
File Download via AJAX
...
codepen.io
html
<button type="button" id="GetFile">Get File!</button>
js
$('#GetFile').on('click', function () {
$.ajax({
url: 'https://s3-us-west-2.amazonaws.com/s.cdpn.io/172905/test.pdf',
method: 'GET',
xhrFields: {
responseType: 'blob'
},
success: function (data) {
var a = document.createElement('a');
var url = window.URL.createObjectURL(data);
a.href = url;
a.download = 'myfile.pdf';
document.body.append(a);
a.click();
a.remove();
window.URL.revokeObjectURL(url);
}
});
});