I'd like to create a progress bar for uploading files using jquery and ajax. so i wrote following jquery code.
function updateProgress(evt)
{
// evt is an ProgressEvent.
if (evt.lengthComputable)
{
var percentLoaded = Math.round((evt.loaded / evt.total) * 100);
// Increase the progress bar length.
$(".progress > div").css(
{
width: percentLoaded + '%'
});
}
}
$.ajax(
{
url: 'assets/php/upload.php?action=uploadFiles',
type: 'POST',
data: newFormData,
cache: false,
xhr: function ()
{
myXhr = $.ajaxSettings.xhr();
if (myXhr.upload)
{
myXhr.upload.addEventListener('progress', updateProgress, false);
}
return myXhr;
},
contentType: false,
processData: false,
});
But the problem is the progress bar will be 100% in a second while uploading file has not finished yet. what is wrong with my code?
Thank you,
Alireza