You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
layui/examples/upload.html

354 lines
9.7 KiB

7 years ago
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
3 years ago
<title>上传组件 - layui</title>
7 years ago
<link rel="stylesheet" href="../src/css/layui.css">
<style>
.layui-upload-img{width: 92px; height: 92px; margin: 0 10px 10px 0;}
hr{margin: 32px 0;}
7 years ago
</style>
</head>
<body class="layui-padding-5">
7 years ago
<div class="layui-upload">
5 years ago
<button type="button" class="layui-btn" id="test1" name="123">上传图片</button>
7 years ago
<div class="layui-upload-list">
<img class="layui-upload-img" src="" id="demo1">
<p id="demoText"></p>
</div>
</div>
5 years ago
<div class="layui-progress layui-progress-big" lay-showPercent="yes" lay-filter="demo">
<div class="layui-progress-bar" lay-percent=""></div>
</div>
7 years ago
<hr>
<div class="layui-upload">
<button type="button" class="layui-btn" id="test2">多图片上传</button>
<div class="layui-upload-list" id="demo2"></div>
</div>
<hr>
<div class="layui-upload">
<button type="button" class="layui-btn layui-btn-normal" id="testList">选择多文件</button>
<div class="layui-upload-list">
<table class="layui-table">
<thead>
<th>文件名</th>
<th>大小</th>
4 years ago
<th>上传进度</th>
7 years ago
<th>操作</th>
</thead>
<tbody id="demoList"></tbody>
</table>
</div>
<button type="button" class="layui-btn" id="testListAction">开始上传</button>
</div>
<hr>
4 years ago
<div class="layui-btn-container">
<button type="button" class="layui-btn test333" lay-data="{size:10,url:'a'}" id="test3"><i class="layui-icon">&#xe67c;</i>上传文件</button>
<button type="button" class="layui-btn layui-btn-primary test333" lay-data="{size:20,url:'b'}" id="test33"><i class="layui-icon">&#xe67c;</i>换个样式</button>
7 years ago
4 years ago
<button type="button" class="layui-btn" id="test4"><i class="layui-icon">&#xe67c;</i>上传视频</button>
<button type="button" class="layui-btn" id="test5"><i class="layui-icon">&#xe67c;</i>上传音频</button>
</div>
7 years ago
<hr>
4 years ago
<div class="layui-btn-container">
<button class="layui-btn testm" lay-data="{url: '/a/'}">参数设在元素上</button>
<button class="layui-btn testm" lay-data="{url: '/b/', accept: 'file',size:5}">参数设在元素上</button>
</div>
7 years ago
<hr>
4 years ago
<div class="layui-btn-container">
7 years ago
<button type="button" class="layui-btn layui-btn-normal" id="test6">选择文件</button>
<button type="button" class="layui-btn" id="test7">开始上传</button>
</div>
<hr><br>
4 years ago
<div class="layui-upload-drag" id="test8">
7 years ago
<i class="layui-icon">&#xe67c;</i>
<p>点击上传,或将文件拖拽到此处</p>
</div>
<hr><br>
4 years ago
绑定原始文件域:<input type="file" name="file" id="test9">
7 years ago
<script src="../src/layui.js" src1="https://cdn.staticfile.org/layui/2.7.6/layui.js"></script>
7 years ago
<script>
layui.use(['upload', 'element', 'form'], function(){
var $ = layui.$;
var upload = layui.upload;
var element = layui.element;
7 years ago
// 模拟接口
var url = '' // 'https://httpbin.org/post';
// 创建实例
7 years ago
var uploadInst = upload.render({
elem: '#test1',
url: url, // 若需模拟上传过程,而不真实上传文件,可使用开源的 HTTP 模拟接口: httpbin
// size: 2000, //限制文件大小,单位 KB
// accept: 'file',
method: 'get',
fileAccept: 'image/*',
exts: 'jpg|png|gif|bmp|jpeg|pdf',
data: { // 额外参数
a: 1,
b: function(){
7 years ago
return 2
}
},
choose: function(obj){
3 years ago
console.log('choose', obj);
},
before: function(obj){
7 years ago
//预读本地文件示例不支持ie8
obj.preview(function(index, file, result){
$('#demo1').attr('src', result); //图片链接base64
});
4 years ago
return;
layer.msg('不允许上传')
return false;
},
done: function(res, index){
6 years ago
7 years ago
//如果上传失败
if(res.code > 0){
return layer.msg('上传失败');
}
//上传成功
3 years ago
console.log(res, index);
},
error: function(index, upload){
6 years ago
this.item.html('重选上传');
7 years ago
//演示失败状态,并实现重传
var demoText = $('#demoText');
demoText.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-mini demo-reload">重试</a>');
demoText.find('.demo-reload').on('click', function(){
uploadInst.upload();
});
5 years ago
element.progress('demo', '0%');
},
progress: function(n, elem, res, index){
3 years ago
console.log(n + '%', elem, res, index); //获取进度百分比
5 years ago
element.progress('demo', n + '%'); //可配合 layui 进度条元素使用
7 years ago
}
});
// 重载上述实例
6 years ago
uploadInst.reload({
field: 'avatar',
accept: 'images', // 只允许上传图片
acceptMime: 'image/*', // 只筛选图片
// size: 2,
6 years ago
});
3 years ago
// 演示多图片上传
7 years ago
upload.render({
elem: '#test2',
url: url, // 实际使用时改成您自己的上传接口即可
multiple: true, // 多文件
unified: true, // 一起上传 --- 2.8.8+
accept: 'images',
number: 3, // 同时上传的数量
size: 1024,
before: function(obj){
// 预读本地文件示例不支持ie8
7 years ago
obj.preview(function(index, file, result){
$('#demo2').append('<img src="'+ result +'" alt="'+ file.name +'" class="layui-upload-img">')
});
3 years ago
// this.files = obj.pushFile();
},
done: function(res, index){
// 上传完毕
console.log(res, index)
},
allDone: function(obj){
7 years ago
console.log(obj)
}
7 years ago
});
// 演示多文件列表 --- 本示例仅演示未开启 unified 属性的情况
7 years ago
var demoListView = $('#demoList');
var uploadListIns = upload.render({
elem: '#testList',
url: url, // 实际使用时改成您自己的上传接口即可
accept: 'file',
multiple: true,
number: 5,
auto: false,
bindAction: '#testListAction',
// size: 30,
choose: function(obj){
var files = this.files = obj.pushFile(); // 将每次选择的文件追加到文件队列
7 years ago
//读取本地文件
3 years ago
obj.preview(function(index, file, result){
7 years ago
var tr = $(['<tr id="upload-'+ index +'">'
,'<td>'+ file.name +'</td>'
,'<td>'+ (file.size/1014).toFixed(1) +'kb</td>'
4 years ago
,'<td><div class="layui-progress" lay-filter="progress-'+ index +'"><div class="layui-progress-bar" lay-percent=""></div></div></td>'
7 years ago
,'<td>'
4 years ago
,'<button class="layui-btn layui-btn-xs demo-reload layui-hide">重传</button>'
,'<button class="layui-btn layui-btn-xs layui-btn-danger demo-delete">删除</button>'
7 years ago
,'</td>'
,'</tr>'].join(''));
//单个重传
tr.find('.demo-reload').on('click', function(){
obj.upload(index, file);
});
//删除
var that = this;
7 years ago
tr.find('.demo-delete').on('click', function(){
console.log(files, index);
7 years ago
delete files[index]; //删除对应的文件
tr.remove();
uploadListIns.config.elem.next()[0].value = ''; //清空 input file 值,以免删除后出现同名文件不可选
7 years ago
});
demoListView.append(tr);
4 years ago
element.render('progress');
7 years ago
});
},
done: function(res, index, upload){
4 years ago
//if(res.code == 0){ //上传成功
7 years ago
var tr = demoListView.find('tr#upload-'+ index)
,tds = tr.children();
tds.eq(3).html(''); //清空操作
delete this.files[index]; //删除文件队列已经上传成功的文件
7 years ago
return;
4 years ago
//}
7 years ago
this.error(index, upload);
},
allDone: function(obj){
7 years ago
console.log(obj)
},
error: function(index, upload){
7 years ago
var tr = demoListView.find('tr#upload-'+ index)
,tds = tr.children();
tds.eq(3).find('.demo-reload').removeClass('layui-hide'); //显示重传
},
progress: function(n, elem, e, index){
4 years ago
console.log(n);
console.log(index);
4 years ago
element.progress('progress-'+ index, n + '%'); //进度条
}
7 years ago
});
upload.render({
elem: '.test333',
url: url,
accept: 'file',
choose: function(obj){
console.log(this.elem);
},
done: function(res){
console.log(res);
7 years ago
}
});
7 years ago
upload.render({
elem: '.testm',
done: function(res, index, upload){
7 years ago
//获取当前触发上传的元素,一般用于 elem 绑定 class 的情况,注意:此乃 layui 2.1.0 新增
var item = this.item;
7 years ago
}
})
/*
7 years ago
upload.render({
elem: '#test33',
url: '',
accept: 'file',
done: function(res){
7 years ago
console.log(res)
}
7 years ago
});*/
7 years ago
upload.render({
elem: '#test4',
url: url, // 实际使用时改成您自己的上传接口即可
accept: 'video',
done: function(res){
7 years ago
console.log(res)
}
});
upload.render({
elem: '#test5',
url: '',
accept: 'audio',
done: function(res){
7 years ago
console.log(res)
}
});
7 years ago
//手动上传
7 years ago
upload.render({
elem: '#test6',
url: url, // 实际使用时改成您自己的上传接口即可
auto: false,
// multiple: true,
bindAction: '#test7',
choose: function(obj){
7 years ago
var that = this;
obj.preview(function(index, file){
that.elem.after('<span class="layui-inline layui-upload-choose">'+ file.name +'</span>');
4 years ago
console.log(file.name);
3 years ago
//obj.resetFile(index, file, '123.jpg');
7 years ago
});
},
before: function(){
7 years ago
console.log(345);
},
done: function(res){
7 years ago
console.log(res);
7 years ago
}
});
upload.render({
elem: '#test8',
url: url, // 实际使用时改成您自己的上传接口即可
done: function(res){
7 years ago
console.log(res);
7 years ago
}
});
upload.render({
elem: '#test9',
url: url, // 实际使用时改成您自己的上传接口即可
done: function(res){
7 years ago
console.log(res);
7 years ago
}
});
4 years ago
7 years ago
});
</script>
</body>
</html>