ajax上传多个文件并显示进度条

3 篇文章 0 订阅
2 篇文章 0 订阅
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>  </title>
    <link href="css/StyleCss.css" rel="stylesheet" type="text/css" />
    <link rel="stylesheet" type="text/css" href="jeasyui/themes/default/easyui.css">
    <link rel="stylesheet" type="text/css" href="jeasyui/themes/icon.css">
    <script type="text/javascript" src="jeasyui/jquery.min.js"></script>
    <script type="text/javascript" src="jeasyui/jquery.easyui.min.js"></script>
    <script type="text/javascript" src="jeasyui/locale/easyui-lang-zh_CN.js"></script>
    <style>
        .processBar{
            display: inline-block;
            width: 0;
            height: 7px;
            /*position: absolute;*/
            /*left: 500px;*/
            /*top: 14px;*/
            margin-bottom: -4px;
            margin-left: 135px;
            background: #009393;
        }
        .processNum {
            position: absolute;
            left: 620px;
            color: #009393;
            font-size: 12px;
            line-height: 35px;
        }
    </style>
    <script language="javascript">
        $(document).ready(function(){
            //上传准备信息
            $("#file").change(function(){
                var file = document.getElementById('file');
                var fileNum = file.files.length;
                $('.processBar').css("width",0);
                //验证要上传的文件
                $('#choose_button').linkbutton({text:fileNum+'个文件'});
                })
            });
        //选择上传视频
        function choose_videos(){
            $('#file').click();
        }
        //上传视频
        function upload_videos(){
            var formData = new FormData($('#uploadForm')[0]);
            var username=$('#username').val();
            var file = document.getElementById('file');
            var fileNum = file.files.length;
            if(!username){
                alert('未输入用户名');
                return false;
            }
            if(fileNum==0){
                alert('未选择文件');
                return false;
            }
            formData.append("username",username);
            console.log(formData);
            $.ajax({
                type: 'post',
                url: "agentVideoManageUpload.php",
                data: formData,
                cache: false,
                processData: false,
                contentType: false,
                xhr:function(){//获取上传进度
                    myXhr = $.ajaxSettings.xhr();
                    if(myXhr.upload){
                        myXhr.upload.addEventListener('progress',function(e){//监听progress事件
                            var loaded = e.loaded;//已上传
                            var total = e.total;//总大小
                            var percent = Math.floor(100*loaded/total);//百分比
                            $('.processNum').text(percent+"%");//数显进度
                            $('.processBar').css("width",percent+"px");//图显进度}, false);
                        });
                        return myXhr;
                    }
                },
                success: function(data){//上传成功回调
                    alert(data);
                    location.reload();
                }


            })
        }
        //清除form
        function clearForm(){
            $('#uploadForm').form('clear');
        }

    </script>

</head>
<body>


                <div id="p" class="easyui-panel" title="本地视频上传" style="width: 100%;height:655px;padding:10px;">
                    <form id="uploadForm" enctype="multipart/form-data">
                    <div cellpadding="5" style="margin:auto;width:60%;">
                        <div style="margin-bottom:20px">
                            <div>用户名:</div>
                            <br/>
                            <input class="easyui-textbox" data-options="prompt:'用户名'" id="username" style="width:100%;height:32px">
                        </div>

                        <div style="margin-bottom:20px">
                                <div style="display: none;">
                                    文件:<input id="file"  type="file" name="file[]" multiple="multiple"/>
                                </div>
                            <a href="#" class="easyui-linkbutton" id="choose_button" style="width: 150px;" onclick="choose_videos()">选择视频</a>
                            <span class="processBar"></span>
                            <span class="processNum">未选择文件</span>
                        </div>

                        <div style="margin-bottom:20px">

                            <a href="#" class="easyui-linkbutton" onclick="upload_videos()" style="width:120px;">提交</a>
                            <a href="#" class="easyui-linkbutton" onclick="clearForm()" style="width:120px;">取消</a>
                        </div>
                    </div>
                    </form>
                </div>


            </div>
  
</body>
</html>

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值