Josh
Josh

Reputation: 1

Upload file in browser and save it to my hard drive again

This may seem pointless but how would I go about coding in JavaScript... I want to upload an image from my pc to my browser and then save it back to my pc in a different location.

Upvotes: 0

Views: 188

Answers (1)

guest271314
guest271314

Reputation: 1

You can use <input type="file"> element, change event, URL.createObjectURL(), <a> element, download attribute, .click()

<input type="file" />
<script>
  document.querySelector("input[type=file]")
    .addEventListener("change", function(e) {
      var a = document.createElement("a");
      a.href = URL.createObjectURL(e.target.files[0]);
      a.download = e.target.files[0].name;
      document.body.appendChild(a);
      a.click();
      document.body.removeChild(a);
    })
</script>

Upvotes: 2

Related Questions