html 代码
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<title>demo</title>
<link rel="stylesheet" href="__STATIC__/layui-v2.5.4/layui/css/layui.css">
</head>
<body>
<div id="demo"></div>
</body>
<script src="__STATIC__/layui-v2.5.4/layui/layui.js"></script>
<script src="__STATIC__/jquery-3.1.1.min.js" type="text/javascript"></script>
</html>
js 代码
$.ajax({
type: 'post',
url: '请求地址',
data: file,
dataType: 'json',
async: true,
contentType: false,
processData: false,
success: success,
beforeSend: function () {
layerIndex = layer.open({
type: 1,
title: false,
closeBtn: 0,
shade: 0.2,
area: ['auto'],
skin: 'layui-layer-nobg', //没有背景色
shadeClose: false,
content: "<div style='width:150px;height:60px;line-height:60px;text-align:center;background:#fff;color:red;font-weight:bold;' id='progressbar_layer'></div>"
});
},
xhr: function () {
var xhr = new XMLHttpRequest();
xhr.upload.addEventListener('progress', function (e) {
//外部资源加载过程中触发,默认500毫秒。
var progressRate = Math.round((e.loaded / e.total) * 100, -2) + '%';
$("#progressbar_layer").html("上传进度:" + progressRate)
})
xhr.upload.addEventListener('load', function (e) {
//外部资源加载成功时触发。如果后台处理时间过长,可以在此提示“后台正在处理请稍等”
})
return xhr;
},
complete: function () {
layer.close(layerIndex)
},
error: function (error) {
layer.msg(error)
}
})