RAM 메모리에 파일을 저장하지 않고 XMLHttpRequest 또는 fetch을 사용하여 큰 파일을 다운로드해야합니다.JavaScript로 큰 파일을 다운로드하는 방법
요청 헤더에 무기명 토큰을 보내야하므로 정상적인 다운로드 링크가 작동하지 않습니다.
파일을 다운로드 할 수 있었지만이 "솔루션"은 저장 대화 상자가 나타나기 전에 파일을 RAM 메모리에 먼저 저장하므로 파일이 커질수록 브라우저가 중단됩니다 RAM 메모리.
var myHeaders = new Headers();
myHeaders.append('Authorization', `Bearer ${token}`);
var myInit = { method: 'GET',
headers: myHeaders,
mode: 'cors',
cache: 'default' };
var a = document.createElement('a');
fetch(url,myInit)
.then((response)=> {
return response.blob();
})
.then((myBlob)=> {
a.href = window.URL.createObjectURL(myBlob);
var attr = document.createAttribute("download");
a.setAttributeNode(attr);
a.style.display = 'none';
document.body.appendChild(a);
a.click();
a.remove();
});
을 그리고 여기 XMLHttpRequest를 내 "솔루션"입니다 : 여기
내 "솔루션"을 페치되어 사용 가능한 RAM 다음 var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange =()=>{
if (xhttp.readyState == 4){
if ((xhttp.status == 200) || (xhttp.status == 0)){
var a = document.createElement('a');
a.href = window.URL.createObjectURL(xhttp.response); // xhr.response is a blob
var attr = document.createAttribute("download");
a.setAttributeNode(attr);
a.style.display = 'none';
document.body.appendChild(a);
a.click();
a.remove();
}
}
};
xhttp.open("GET", url);
xhttp.responseType = "blob";
xhttp.setRequestHeader('Authorization', `Bearer ${token}`);
xhttp.send();
문제는 어떻게 다운로드 할 수 큰 파일 - 메모리와 같은 시간에 헤더를 설정?
여기에서 답변을 찾을 수 있습니다. https://stackoverflow.com/questions/4545311/download-a-file-by-jquery-ajax – juancab
@juancab jQuery 파일 다운로드 솔기가 해결책이 될 수는 없습니다. 심지어 내 머리글 (무기명 토큰), 내가 틀리지 않으면, jQuery를 보냅니다. 파일 다운로드 솔기는 내가 이미 해결 한 바로 그 해결책 일뿐입니다. 테스트를 거치지는 않았지만 jQuery 파일 다운로드에는 내가 가지고있는 것과 같은 문제가있을 것이라고 생각합니다. RAM 메모리보다 큰 파일을 다운로드하는 것은 틀렸을 때 정정 해줍니다. – Sheki