SeonHyungJo / Tip-Note

:round_pushpin: 개발을 하면서 느끼고 알게된 Tip:round_pushpin:
7 stars 0 forks source link

내가 업로드한 파일 내가 다운받기 #49

Open SeonHyungJo opened 4 years ago

SeonHyungJo commented 4 years ago
<!DOCTYPE html>
<html>
<head></head>
<body>
  <input type="file" id="filebtn" value="upload" />
  <input type="button" id="btn" value="Download" />
</body>
<script>
  function download(file) {
    const name = file.name;
    const blob = new Blob([file]);
    const url = URL.createObjectURL(blob);
    const element = document.createElement('a');

    element.setAttribute('href', url);
    element.setAttribute('download', name);

    document.body.appendChild(element);

    element.click();

    document.body.removeChild(element);
  }

  document.getElementById("btn").addEventListener("click", function () {
    const file = document.getElementById("filebtn").files
    download(file[0]);
  })
</script>

</html>