Skip to content

Instantly share code, notes, and snippets.

@code-boxx
Created June 30, 2023 03:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save code-boxx/4b570351df81be1878087ad0fb3cb2ed to your computer and use it in GitHub Desktop.
Save code-boxx/4b570351df81be1878087ad0fb3cb2ed to your computer and use it in GitHub Desktop.
Javascript File Upload Progress Bar

JAVASCRIPT FILE UPLOAD PROGRESS BAR

https://code-boxx.com/ajax-upload-progress-bar/

NOTES

  1. Accessing upload.html use http://. NOT file://.
  2. The server-side upload handler is in PHP, you need to create your own upload handler if using other languages.

LICENSE

Copyright by Code Boxx

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

/* (A) WRAPPER */
#up-wrap, #up-wrap * {
font-family: Arial, Helvetica, sans-serif;
box-sizing: border-box;
}
#up-wrap {
max-width: 400px;
padding: 20px;
margin: 20px;
background: #f1f1f1;
}
#up-wrap h2 { margin: 0 0 15px 0; }
/* (B) PROGRESS BAR */
#up-progress, #up-bar, #up-percent { height: 30px; }
#up-progress {
position: relative;
background: #fff;
}
#up-bar {
background: #d3e3ff;
width: 0;
transition: width 0.5s;
}
#up-percent {
position: absolute; top: 0; left: 0;
width: 100%; display: flex;
align-items: center; justify-content: center;
}
/* (C) FILE PICKER */
#up-file { display: none; }
#up-label {
display: inline-block;
margin-top: 20px;
padding: 10px 20px;
color: #fff;
border: 1px solid #dfdfdf;
background: #296cd0;
cursor: pointer;
}
#up-file:disabled ~ #up-label { background: #b1b1b1; }
/* (X) DOES NOT MATTER */
body { padding: 0; margin: 0; }
<!DOCTYPE html>
<html>
<head>
<title>AJAX Upload</title>
<link rel="stylesheet" href="upload.css">
<script src="upload.js"></script>
</head>
<body>
<div id="up-wrap">
<h2>UPLOAD FILE</h2>
<!-- (A) PROGRESS BAR -->
<div id="up-progress">
<div id="up-bar"></div>
<div id="up-percent">0%</div>
</div>
<!-- (B) FILE PICKER -->
<input type="file" id="up-file" disabled>
<label for="up-file" id="up-label">
Select File
</label>
</div>
</body>
</html>
var uprog = {
// (A) INIT
hBar : null, // html progress bar
hPercent : null, // html upload percentage
hFile : null, // html file picker
init : () => {
// (A1) GET HTML ELEMENTS
uprog.hBar = document.getElementById("up-bar");
uprog.hPercent = document.getElementById("up-percent");
uprog.hFile = document.getElementById("up-file");
// (A2) ATTACH AJAX UPLOAD + ENABLE UPLOAD
uprog.hFile.onchange = uprog.upload;
uprog.hFile.disabled = false;
},
// (B) HELPER - UPDATE PROGRESS BAR
update : percent => {
percent = percent + "%";
uprog.hBar.style.width = percent;
uprog.hPercent.innerHTML = percent;
if (percent == "100%") { uprog.hFile.disabled = false; }
},
// (C) PROCESS UPLOAD
upload : () => {
// (C1) GET FILE + UPDATE HTML INTERFACE
let file = uprog.hFile.files[0];
uprog.hFile.disabled = true; // disable upload button
uprog.hFile.value = ""; // reset file picker
// (C2) AJAX UPLOAD
let xhr = new XMLHttpRequest(),
data = new FormData();
data.append("upfile", file);
xhr.open("POST", "upload.php");
// (C3) UPLOAD PROGRESS
let percent = 0, width = 0;
xhr.upload.onloadstart = evt => uprog.update(0);
xhr.upload.onloadend = evt => uprog.update(100);
xhr.upload.onprogress = evt => {
percent = Math.ceil((evt.loaded / evt.total) * 100);
uprog.update(percent);
};
// (C4) ON LOAD & ERRORS
xhr.onload = function () {
if (this.response!= "OK" || this.status!=200) {
// @TODO - DO SOMETHING ON ERROR
// alert("ERROR!");
// reset form?
console.log(this);
console.log(this.response);
console.log(this.status);
} else {
uprog.update(100);
// @TODO - DO SOMETHING ON COMPLETE
}
};
xhr.onerror = err => console.error(err);
// (C5) GO!
xhr.send(data);
}
};
window.addEventListener("load", uprog.init);
<?php
// (A) MOVE UPLOADED FILE
$source = $_FILES["upfile"]["tmp_name"];
$destination = $_FILES["upfile"]["name"];
move_uploaded_file($source, $destination);
// (B) RESPONSE
echo "OK";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment