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.?