文件上传进度条显示

<!DOCTYPE html>
<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>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值