Code With Mark
Home
About
Resources
Contact

Easily Create File upload progress bar using only javascript

Want to learn how to easily create javascript upload ajax with progress bar?

In this tutorial, you will learn how to easily create a progress bar during the AJAX file upload process.

Your progress bar width will gradually increased based on the progress completed.

Here is HTML form for file upload

<form method="post" enctype="multipart/form-data">
    <input type="file" name="image" id="image" onchange="upload()"><br>
    <progress id="progressBar" value="0" max="100" style="width:250px;"></progress>
    <h2 id="status"></h2>
    <p id="loadedtotal"></p>
</form>

Here is the javascript code for progress bar using ajax

<script type="text/javascript">

    function _(el) {
        return document.getElementById(el);
    }

    function upload() {
        var file = _("image").files[0];
        var formdata = new FormData();
        formdata.append("image", file);
        var ajax = new XMLHttpRequest();
        ajax.upload.addEventListener("progress", progressHandler, false);
        ajax.addEventListener("load", completeHandler, false);
        ajax.addEventListener("error", errorHandler, false);
        ajax.addEventListener("abort", abortHandler, false);
        ajax.open("POST", "file_upload.php"); 
        ajax.send(formdata);
    }

    function progressHandler(event) {
        _("loadedtotal").innerHTML = "Uploaded " + event.loaded + " bytes of " + event.total;
        var percent = (event.loaded / event.total) * 100;
        _("progressBar").value = Math.round(percent);
        _("status").innerHTML = Math.round(percent) + "% uploaded... please wait";
    }

    function completeHandler(event) {
        _("status").innerHTML = event.target.responseText;
        _("progressBar").value = 0;
    }

    function errorHandler(event) {
        _("status").innerHTML = "Upload Failed";
    }

    function abortHandler(event) {
        _("status").innerHTML = "Upload Aborted";
    }
</script>

Here is the file upload php code

if (move_uploaded_file($_FILES['image']['tmp_name'], 'image/' . $_FILES['image']['name'])) {
    echo 'Successfully Uploaded!';
} else {
    echo 'Error in file upload.';
}
For Web Developers

Your Next Project Could Make You Money for Years

Most web developers get paid once for the work they do.

But what if you could use those same skills to build something that continues generating income long after it's launched?

A simple SaaS, plugin, web app, or digital product can keep bringing in customers and revenue month after month.

Instead of starting from zero with every new client project, you can create assets that work for you—even when you're not.

Learn How To Build Monthly Income →
How To Easily Detect Mobile Devices For Your Web ApplicationsHow To Easily Detect Mobile Devices For Your Web Applications←Previous
New Safer & Faster Web BrowserNew Safer & Faster Web BrowserNext→

Related Posts

  • How Google Developers Think (And Why You Should Too)
  • Add Google Sign-In in 2 Minutes
  • Form Validation in 1 Line

Top Posts Viewed

Google Sheets Timesheet Website App Script
2.18K views
Learn To Create YouTube Video Downloader
1.85K views
Easily Edit HTML Table Rows Or Cells With jQuery
1.53K views

Categories

Courses
Excel
Google Script
Javascript
jQuery
Microsoft Access
MongoDB
Node JS
PHP
Quick Tip
Uncategorized
Wordpress