Skip to content

Instantly share code, notes, and snippets.

@Piterden
Last active May 11, 2020 22:56
Show Gist options
  • Save Piterden/e1828597c682362c1f51c6876d57ae0a to your computer and use it in GitHub Desktop.
Save Piterden/e1828597c682362c1f51c6876d57ae0a to your computer and use it in GitHub Desktop.
const send = (file) => {
const xhr = new XMLHttpRequest();
const formData = new FormData();
formData.append("avatar", file);
xhr.open("POST", "/", true);
xhr.upload.onprogress = function(event) {
if (event.lengthComputable) {
const percent = (event.loaded / event.total) * 100;
document.getElementById("output").innerText = percent + "%";
}
};
xhr.send(formData);
};
const click = () => {
const file = document.getElementById("file").files[0];
send(file);
};
document.getElementById("btn").addEventListener("click", click);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment