我有一个简单的PHP上传表单,包含AJAX和jQuery。我想显示上传进度。
var main = function(e) {
$("#upload").on('submit', function(e) {
e.preventDefault();
$("#l").show();
$.ajax({
type: 'POST',
url: 'upload.php',
data: new FormData(this),
contentType: false,
processData: false,
success: function(data) {
$("#l").hide();
},
});
});
};
$(document).ready(main);
<form id="upload" method="POST" enctype="multipart/form-data">
<input type="file" name="file">
<input type="submit">
</form>
<div id="here"></div>
<div id="l" style="display:none;">Loading...</div>
<div id="p"></div>
如何显示上传进度?
答案 0 :(得分:1)
有一组选项可以在$(form).ajaxSubmit()中跟踪AJAX调用的进度。
$(document).ready(function() {
$('#upload').submit(function(e) {
if($('#userImage').val()) {
e.preventDefault();
$('#loader-icon').show();
$(this).ajaxSubmit({
target: '#targetLayer',
beforeSubmit: function() {
$("#progress-bar").width('0%');
},
uploadProgress: function (event, position, total, percentComplete){
$("#progress-bar").width(percentComplete + '%');
$("#progress-bar").html('<div id="progress-bar-status">' + percentComplete +' %</div>')
},
success:function (){
$('#loader-icon').hide();
},
resetForm: true
});
return false;
}
});
});