文件上传加进度条,layer 弹出进度

7 篇文章 0 订阅
2 篇文章 0 订阅

CSS

.process-bar-wrapper{
	background-color: #FFF;
    width: 360px;
    height: 34px;
    position: relative;
    padding: 0;
    margin: 0;
    overflow: hidden;
    border-radius: 2px;
}
.process-bar-wrapper .title{
	background-color: #FFF;
    width: 360px;
    height: 20px;
    line-height: 20px;
    position: relative;
    padding: 0;
    margin: 0;
    border-radius: 5px;
    font-size: 12px;
    text-align: center;
    padding: 0;
    margin:0;
    border-radius: 2px;
}
.process-bar-wrapper .bar{
	background-color: #FF8828;
	height: 14px;
	display: inline-block;
	padding: 0;
	margin: 0;
	position:absolute;
	top: 20px;
	left: 0;
	opacity: 1;
}

js 源码 


/*
* 上传文件
*/ 
function ajaxUpload() {
    var file_obj = document.getElementById('file_upload').files[0];
	var _token = $('meta[name="csrf-token"]').attr('content');
	var url = '/upload/server?_token='+_token;
    var data = new FormData();
    data.append('upfile', file_obj);
    $.ajax({
        type:"post",
        async:true,  //这里要设置异步上传,才能成功调用myXhr.upload.addEventListener('progress',function(e){}),progress的回掉函数
        Accept:'text/html;charset=UTF-8',
        data:data,
        contentType:"multipart/form-data",
        url: url,
        processData: false, // 告诉jQuery不要去处理发送的数据
        contentType: false, // 告诉jQuery不要去设置Content-Type请求头
        beforeSend: function (XMLHttpRequest) {
            var _itemEle = '<div class="process-bar-wrapper">'+
            '<p  class="title" id="processTitle" >上传中,请稍等...</p>'+
            '<p  class="bar" id="processBar">&nbsp;</p>'+
            '</div>';
            layer.open({
              type: 1
              ,title: false
              ,closeBtn:0
              ,resize:false
              ,shadeClose: false //点击遮罩区域是否关闭页面
              ,shade: .50
              ,content:_itemEle
            })
        },
        xhr:function(){
            myXhr = $.ajaxSettings.xhr();
            if(myXhr.upload){ // check if upload property exists
                myXhr.upload.addEventListener('progress',function(e){    
                    var loaded = e.loaded;                  //已经上传大小情况 
                    var total = e.total;                      //附件总大小 
                    var percent = Math.floor(100*loaded/total)+"%";     //已经上传的百分比              
                    $("#processBar").css("width",percent);
                    $("#processTitle").text("上传中,已完成("+percent+")"); 
                               
                }, false); // for handling the progress of the upload
            }
            return myXhr;
        },
        success:function (res) {
            layer.closeAll();
            $('#file_upload').val('');
			if(res && res.state == '200''){
                layer.msg('上传成功!');
            }else{
            	layer.msg('上传失败!'+res.state);
            }
        },
        error: function () {
            layer.closeAll();
            $('#file_upload').val('');
            layer.msg('上传失败!');
        },

    })
}

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值