1

I'm creating a message sending interface along with single file upload (few text field and text areas with one file upload button). I want to show a progress bar while sending message.

I wrote a js function to send message using jquery ajax and it is working fine.

now i m trying to attach progress event. for that i modified my jquery ajax code using google and stack overflow as below:

var form = new FormData(document.getElementById('frm_send_msg'));
    var file = document.getElementById('attachment').files[0];
    if (file) {
        form.append('attachment', file);
    }
    $.ajax({
        url: '/send_msg.php',
        type: 'POST',
        async: false,
        cache: false,
        dataType: 'json',
        contentType: false,
        processData: false,
        data: form,
        xhr: function() {  // custom xhr
            var myXhr = $.ajaxSettings.xhr();
            if (myXhr.upload) { // check if upload property exists
                myXhr.upload.addEventListener('progress', progressHandlingFunction, false); // for handling the progress of the upload
            }
            return myXhr;
        },
        success: function(data) {
            //code to hide sending interface
        },
        complete: function() { //     
           alert("complete");
        },
        error: function() {

            alert("ERROR in sending message");
        }
    }) 

And I wrote a function to handle progress event:

function progressHandlingFunction(evt) {
    console.log('updating fun called');
    // evt is an ProgressEvent.
    if (evt.lengthComputable) {
        console.log('updating');
        var percentLoaded = Math.round((evt.loaded / evt.total) * 100);
        // Increase the progress bar length.
        $(".progress > div").css({
            width: percentLoaded + '%'
        });

    } else {
        console.log('not updating');
    }
}

But i m neither getting any of one console log entry from progressHandlingFunction function nor my .progress div shows any changes.

need help to solve this please!

This is what console is showing in chrome

>XHR finished loading: "http://localhost/send_msg.php". 
  send jquery.js:6
  x.extend.ajax jquery.js:6
  send_msg custom_scripts.js:402
  onclick

One more thing i want to mention here:

My form is inside twitter bootstrap3.0 modal.?

3
  • 1
    What browser are you testing in, just to be sure. Commented Dec 19, 2013 at 15:46
  • 1
    @Ravi: Are you getting any console errors or warnings? Can you please post your HTML code? Commented Dec 20, 2013 at 7:11
  • I modified question see below Commented Dec 20, 2013 at 7:18

1 Answer 1

1

I am not sure will it work or not but modify your ajax code to this and try try removing success :function(){} from ajax.

$.ajax({
        url: '/send_msg.php',
        type: 'POST',
        async: false,
        cache: false,
        dataType: 'json',
        contentType: false,
        processData: false,
        data: form,
        xhr: function() {  // custom xhr
            var myXhr = $.ajaxSettings.xhr();
            if (myXhr.upload) { // check if upload property exists
                myXhr.upload.addEventListener('progress', progressHandlingFunction, false); // for handling the progress of the upload
            }
            return myXhr;
        },
        complete: function() { //     
//code to hide sending interface
           alert("complete");
        },
        error: function() {

            alert("ERROR in sending message");
        }
    });

Hope it helps you.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks you mr/ms/mrs ultimate, it worked like charm.. :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.