Skip to main content

File Upload Progess Bar with JavaScript HTML PHP

In this tutorial, we will see how to create a file upload functionality with progress bar. Progress bar tells us, how much content is uploaded using normal HTML, jQuery, AJAX and PHP (as a back end). I have used a custom CSS styling to create a progress bar and to design other markup elements like list of uploaded images.

File Upload Progess Bar with JavaScript HTML PHP


Before starting this, you should have a basic knowledge of all above mentioned technologies to understand it properly.

Following are the steps:

To create HTML markup using bootstrap: 

Below code is a markup for file upload functionality. Create a new file and save it with the name file-upload-progres-bar.html and copy below code in the same file.

<!DOCTYPE html>
<html>
<head>
    <title>File Upload with Progress Bar</title>
    <!-- Custom stylesheet -->
    <link rel="stylesheet" type="text/css" href="upload-file-progress-bar-style.css">
</head>
<body>
    <div class="container">
        <!-- heading -->
        <h1>File Upload</h1>
        <form action="upload-file.php" method="post" enctype="multipart/form-data" id="upload_file_form" name="upload_file_form">
            <!-- input field -->
            <input type="file" name="files" id="files">
        </form>

        <!-- progress bar of upload status with progress in percent -->
        <div id="progress-files">
            <div class="progress-bar-files"></div >
            <div class="status-files">0%</div>
        </div>
        <!-- list of the files uploaded -->
        <div class="files-list"></div>
    </div>

    <script type="text/javascript" src="jquery.min.js"></script>
    <script type="text/javascript" src="file-upload.js"></script>
</body>
</html>


 To create a JavaScript code using an Ajax to handle file upload from client end (browser):

Now, create a JavaScript file and name it as file-upload.js and Copy the below code in it.

// files upload
        $("#files").on("change", function(){
            $.ajax({
                url: $("#upload_file_form").attr("action"),
                data: new FormData($("#upload_file_form")[0]), // $(form).serialize(),
                type: "post",
                dataType: "JSON",
                cache: false,
                contentType: false,
                processData: false,
                xhr: function(){
                    //upload Progress
                    var xhr = $.ajaxSettings.xhr();
                    if (xhr.upload) {
                        xhr.upload.addEventListener('progress', function(event) {
                            var percent = 0;
                            var position = event.loaded || event.position;
                            var total = event.total;
                            if (event.lengthComputable) {
                                percent = Math.ceil(position / total * 100);
                            }
                    // console.log(percent)
                            //update progressbar
                            $(" .progress-bar-file").css("width", + percent +"%");
                            $(" .status-files").text(percent +"%");
                        }, true);
                    }
                    return xhr;
                },
                mimeType:"multipart/form-data"
               
            }).done(function(response){ //
                if(response.flag){
                    var html = "<a href='" + response.src + "'><img width='200' src='" + response.src + "' alt=''></a>";
                    $(".files-list").append(html);
                }
                // location.reload();
            });
        });


Above code contains the normal ajax call with xhr, which handles the progress of the file upload and shows it in the form of progress bar.

To create a PHP code to handle file upload from server end:

Now, the backend with php. It is useful to save the files either on the server or the database.
<?php
// chekcs whether data for upload exists or not
if(isset($_FILES['files']))
{
  $uploadfile=$_FILES["files"]["tmp_name"]; 
  $folder="uploads/";

  // checks whether file is uploaded or not
  if(move_uploaded_file($_FILES["files"]["tmp_name"], $folder.$_FILES["files"]["name"])){
          echo json_encode(array("flag" => true, "msg" => "File uploaded successfully!", "src" => $folder . $_FILES["files"]['name']));
    } else {
        echo json_encode(array("flag" => false, "msg" => "Failed to upload file..."));
    }
  // echo '<img src="'.$folder."".$_FILES["upload_file"]["name"].'">';
  // exit();
}

?>


So, in this tutorial, we have learnt how to create a file upload functionality with progress bar. Please like and share the post with your friend if you find it useful.

Comments

Popular posts from this blog

CCAvenue avoid Error Code 21002 currency required parameter missing

In this tutorial, we will see how to avoid Error Code 21002 currency required parameter missing. This is a usual problem that occurs after connecting our application to the CCAvenue payment gateway. Due to some input values that are missing by developers while coding, such error occurs. CCAvenue is one of the leading online payment gateway service provider similar to other popular providers PayPal, Citrus, PayUMoney etc. Easy integration is an advantage of CCAvenue payment gateway. But, due to very minor mistakes in coding, developers get few errors while integrating CCAvenue with their application. CCAvenue have provided some guidelines of integration that are necessarily needed to be followed while integrating it into the application. Inputs values like amount, currency, merchant_id, redirect_url, cancel_url etc are minimum required and should be included in the form before sending the data to CCAvenue page. If we avoid them or forgot to use them into the code, CCAvenue th

CCAvenue avoid Error Code 21003 amount required parameter missing

In this tutorial, we will see how to avoid Error Code 21003 amount required parameter missing. This is another problem that occurs due to very minor mistake made by developer after connecting an application to the CCAvenue payment gateway. Due to some input values that are missing by developers while coding, such error occurs. CCAvenue is one of the leading online payment gateway service provider similar to other popular providers PayPal, Citrus, PayUMoney etc. Easy integration is an advantage of CCAvenue payment gateway. As per the guidelines of integration provided by CCAvenue that are necessarily needed to be followed while integrating it into the application. Input values like amount, currency, merchant_id, redirect_url, cancel_url etc are minimum required and should be included in the form before sending the data to CCAvenue page. If we avoid them or forgot to use them into the code, CCAvenue throws an error. As mentioned above, Amount is one of the required values that

CCAvenue avoid Error Code 21001 order id required parameter missing

In this tutorial, we will see how to avoid Error Code 21001 order_id required parameter missing. This is a usual problem that occurs after connecting our application to the CCAvenue payment gateway. But, it is not a big issue to solve. CCAvenue is one of the leading online payment gateway service provider similar to other popular providers PayPal, Citrus, PayUMoney etc. Easy integration is an advantage of CCAvenue payment gateway. But, due to very minor mistakes in coding, developers get few errors while integrating CCAvenue with their application. CCAvenue have provided some guidelines of integration that are necessarily needed to follow while integrating it into the application. It includes some parameters or inputs that are minimum required like amount, currency, merchant_id, redirect_url, cancel_url etc. If we avoid them or forgot to use them into the code, CCAvenue throws the error. As mentioned above Order Id is one of the required values that should be mentioned in co