檔案上傳進度條顯示
<html>
<head>
<meta charset="UTF-8" />
<title>Insert title here</title>
<style>
body { padding: 30px }
form { display: block; margin: 20px auto; background: #eee; border-radius: 10px; padding: 15px }
.progress { position:relative; width:400px; border: 1px solid #ddd; padding: 1px; border-radius: 3px; }
.bar { background-color: #B4F5B4; width:0%; height:20px; border-radius: 3px; }
.percent { position:absolute; display:inline-block; top:3px; left:48%; }
</style>
</head>
<body>
<form id="myForm" action="/uploadLocalVideoAndInfo" method="post" enctype="multipart/form-data">
<div class="col-md-2" style="line-height:25px;text-align:right;margin-right:20px;"><label for="upload-file-input">視訊名稱</label></div>
<input type="text" name="videoName" />
<input type="file" name="uploadFile" /><br>
<div class="col-md-2" style="line-height:25px;text-align:right;margin-right:20px;"><label for="upload-file-input">拍攝地點</label></div>
<input type="text" name="videoPlace" />
<input type="submit" value="Upload File to Server">
</form>
<div class="progress">
<div class="bar"></div >
<div class="percent">0%</div >
</div>
<div id="status"></div>
<script src="http://libs.baidu.com/jquery/2.1.4/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery.form/4.2.2/jquery.form.min.js" integrity="sha384-FzT3vTVGXqf7wRfy8k4BiyzvbNfeYjK+frTVqZeNDFl8woCbF0CYG6g2fMEFFo/i" crossorigin="anonymous"></script>
<!-- <script src="/static/js/jquery.form.js"></script> -->
<script>
$(function(){
var bar = $('.bar');
var percent = $('.percent');
var status = $('#status');
$('#myForm').ajaxForm(
{
beforeSerialize:function(){
//alert("表單資料序列化前執行的操作!");
//$("#txt2").val("java");//如:改變元素的值
},
beforeSubmit:function(){
//alert("表單提交前的操作");
var filesize = $("input[type='file']")[0].files[0].size/1024/1024;
if(filesize > 1000){
alert("檔案大小超過限制,最多1000M");
return false;
}
//if($("#txt1").val()==""){return false;}//如:驗證表單資料是否為空
},
beforeSend: function() {
status.empty();
var percentVal = '0%';
bar.width(percentVal)
percent.html(percentVal);
},
uploadProgress: function(event, position, total, percentComplete) {//上傳的過程
//position 已上傳了多少
//total 總大小
//已上傳的百分數
var percentVal = percentComplete + '%';
bar.width(percentVal)
percent.html(percentVal);
//console.log(percentVal, position, total);
},
success: function(data) {//成功
var percentVal = '100%';
bar.width(percentVal)
percent.html(percentVal);
//alert(data);
},
error:function(err){//失敗
alert("表單提交異常!"+err.msg);
},
complete: function(xhr) {//完成
status.html(xhr.responseText);
}
});
});
</script>
</body>
</html>