연습 - 사용자 파일 다운로드
이 연습에서는 앱에서 다운로드 기능을 완료하여 파일을 다운로드할 파일 이름을 선택할 수 있습니다.
graph.js 파일 끝에 다음 함수를 추가합니다.
async function downloadFile(file) { try { const response = await graphClient .api(`/me/drive/items/${file.id}`) .select('@microsoft.graph.downloadUrl') .get(); const downloadUrl = response["@microsoft.graph.downloadUrl"]; window.open(downloadUrl, "_self"); } catch (error) { console.error(error); } }
ui.js에서
a.href = assignment
문 바로 뒤에 이 줄을 즉시 추가합니다.a.onclick = () => { downloadFile(file); };
완료된
displayFiles()
함수는 다음과 같이 표시됩니다.async function displayFiles() { const files = await getFiles(); const ul = document.getElementById('downloadLinks'); while (ul.firstChild) { ul.removeChild(ul.firstChild); } for (let file of files) { if (!file.folder && !file.package) { let a = document.createElement('a'); a.href = '#'; a.onclick = () => { downloadFile(file); }; a.appendChild(document.createTextNode(file.name)); let li = document.createElement('li'); li.appendChild(a); ul.appendChild(li); } } }
이제 페이지를 새로 고칩니다. 다운로드할 파일을 선택할 수 있어야 합니다.