我想使用jQuery和Ajax创建上传文件的进度条。因此,我编写了以下jQuery代码。
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,
});
但问题是进度条在一秒钟内就会达到100%,而上传文件还没有完成。我的代码有什么问题吗?
谢谢,
Alireza