Skip to content

Instantly share code, notes, and snippets.

@Borodin
Last active November 2, 2023 01:05
Show Gist options
  • Save Borodin/7256178 to your computer and use it in GitHub Desktop.
Save Borodin/7256178 to your computer and use it in GitHub Desktop.
Upload file javascript
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Upload</title>
</head>
<body>
<?php
if($_FILES['image']['tmp_name']){
move_uploaded_file($_FILES['image']['tmp_name'], '/test/images/'.rand(1, 1000).'.jpg');
}
?>
<form enctype="multipart/form-data" method="POST">
<input type="file" name="image">
<input type="button" onclick="upload(this)">
</form>
<form enctype="multipart/form-data" method="POST">
<input type="file" name="image">
<input type="button" onclick="upload(this)">
</form>
<script>
function upload(input){
var xhr = new XMLHttpRequest();
xhr.upload.onprogress = function(e) {
console.log(e.loaded, e.total)
}
xhr.upload.onload = function(e) {
console.log('file upload')
}
xhr.open("POST", "/test/uploadfile.php", true);
xhr.send(new FormData(input.parentElement));
}
</script>
</body>
</html>
@sparkyx
Copy link

sparkyx commented Oct 6, 2019

thanks :-)

@diogomartino
Copy link

It works well, thanks!

@yaskshelat
Copy link

can you add upload progress bar to this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment