user2060312
user2060312

Reputation: 41

How to show progress bar in ajax file upload

My code post ajax request but it is not showing a progress bar. Please help to correct the code to show a working progress bar.

$(document).ready(function () {
    $("#uploadbutton").click(function () {
    var filename = $("#file_path$i").val(); //get form data;

    $.ajax({
      type: "html",
      url: "share.php",//onwhich post ajax data;
      enctype: 'multipart/form-data',
      data: {
        file: filename
      },
      success: function () {
        alert("Data Uploaded: ");
      }
    });
 });
});

Upvotes: 4

Views: 12357

Answers (3)

Samuel Joy
Samuel Joy

Reputation: 608

@whitneyit : we can use xhr: function of ajax to track the file upload something like this

$.ajax({
                url: 'handler/FileUploader.ashx?FileName=' + file.name, //server script to process data
                type: 'POST',
                xhr: function () {
                    myXhr = $.ajaxSettings.xhr();
                    if (myXhr.upload) {
                        myXhr.upload.addEventListener('progress', progressHandlingFunction, false);
                    }
                    return myXhr;
                },
                success: function (result) {                    
                    //On success if you want to perform some tasks.
                },
                data: file,
                cache: false,
                contentType: false,
                processData: false
            });
            function progressHandlingFunction(e) {
                if (e.lengthComputable) {
                    var s = parseInt((e.loaded / e.total) * 100);
                    $("#progress" + currFile).text(s + "%");
                    $("#progbarWidth" + currFile).width(s + "%");
                    if (s == 100) {
                        triggerNextFileUpload();
                    }
                }
            }

Upvotes: 2

grepsedawk
grepsedawk

Reputation: 3411

This is from BlueImp's jQuery-File-Upload:

First of all, download: https://github.com/blueimp/jQuery-File-Upload/archives/master

Now, upload the js folder.

Make your .html:

    <html>
    <head>
    <meta charset="utf-8">
    <title>jQuery File Upload Example</title>
    <style>
     .bar {
         height: 18px;
         background: green;
      }
    </style>
    </head>
    <body>
    <input id="fileupload" type="file" name="files[]" data-url="server/php/" multiple>
    <div id="progress">
       <div class="bar" style="width: 0%;"></div>
    </div>
    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
    <script src="js/vendor/jquery.ui.widget.js"></script>
    <script src="js/jquery.iframe-transport.js"></script>
    <script src="js/jquery.fileupload.js"></script>
    <script>
    $(function () {
        $('#fileupload').fileupload({
            dataType: 'json',
            done: function (e, data) {
                $.each(data.result.files, function (index, file) {
                    $('<p/>').text(file.name).appendTo(document.body);
                });
            }
        });
progressall: function (e, data) {
        var progress = parseInt(data.loaded / data.total * 100, 10);
        $('#progress .bar').css(
            'width',
            progress + '%'
        );
    }
    });
    </script>
    </body> 
    </html>

I have not tested this, but it SHOULD be functional. Let me know if it is not.

Optional: Include the contents of <style></style> in your .css file.

Optional: Include .js in a .js <script src=""></script> tag.

Source: https://github.com/blueimp/jQuery-File-Upload/wiki/Basic-plugin

Upvotes: 1

whitneyit
whitneyit

Reputation: 1226

Unfortunately the $.ajax method does not return any progres information.

Also, there is no crossbrowser solution to this using only HTML and JavaScript. I would suggest looking at Uploadify.

Upvotes: 0

Related Questions