我创建此代码以使用ajax和php上传文件,我想添加 进度条显示上传的百分比。
这是我的代码
<script>
$("form#data").submit(function(){
var formData = new FormData($(this)[0]);
$.ajax({
url: "functions/video.php",
type: 'POST',
data: formData,
async: false,
success: function (data) {
document.getElementById("status").innerHTML = data;
},
cache: false,
contentType: false,
processData: false
});
return false;
});
</script>
<form id="data" method="post" enctype="multipart/form-data">
<input name="up_vid" type="file" id="up_vid"/>
<div class="upload_v_icon"></div>
<div class="video_info">
<input type="text" name="video_title" placeholder="Video title" />
<input type="text" name="tags" placeholder="funny,9gag,nice,crazy ..."/>
<textarea name="description" placeholder="Description"></textarea>
</div>
<div class="bg_upload">
<p>When you upload this video your are agree with <a href="">Terms</a> of service.</p>
<button>Begin Upload</button>
</div>
</form>
谢谢。
答案 0 :(得分:3)
这假设1px宽的gif称为&#34; progress.gif&#34;存在。将其颜色设置为您希望进度条显示的颜色。
将这样的内容添加到您的css:
.uploadBar {
background-image:url(/images/progress.gif);
background-position: -1px;
background-repeat:no-repeat;
background-size=0% 100%; width:100%;
position: relative; overflow: hidden;
}
将这样的内容添加到$ .ajax();
xhr: function() {
var xhr = new window.XMLHttpRequest();
//Upload progress
xhr.upload.addEventListener("progress", function(evt) {
if (evt.lengthComputable) {
var percentComplete = evt.loaded / evt.total;
console.log(percentComplete);
$('.uploadBar').css({ backgroundSize: (percentComplete*100) + '%'});
}
}, false);
return xhr;
}