2017-08-21 08:51:13 +08:00
|
|
|
|
<!DOCTYPE html>
|
|
|
|
|
<html>
|
|
|
|
|
<head>
|
|
|
|
|
<meta charset="utf-8">
|
|
|
|
|
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
|
2022-06-05 23:55:31 +08:00
|
|
|
|
<title>上传组件 - layui</title>
|
2017-08-21 08:51:13 +08:00
|
|
|
|
|
|
|
|
|
<link rel="stylesheet" href="../src/css/layui.css">
|
|
|
|
|
|
|
|
|
|
<style>
|
|
|
|
|
body{padding: 50px 100px;}
|
|
|
|
|
.layui-upload-img{width: 92px; height: 92px; margin: 0 10px 10px 0;}
|
|
|
|
|
hr{margin: 30px 0;}
|
|
|
|
|
</style>
|
|
|
|
|
</head>
|
|
|
|
|
<body>
|
|
|
|
|
|
|
|
|
|
<div class="layui-upload">
|
2020-01-15 06:30:00 +08:00
|
|
|
|
<button type="button" class="layui-btn" id="test1" name="123">上传图片</button>
|
2017-08-21 08:51:13 +08:00
|
|
|
|
|
|
|
|
|
<div class="layui-upload-list">
|
|
|
|
|
<img class="layui-upload-img" src="" id="demo1">
|
|
|
|
|
<p id="demoText"></p>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
|
2019-09-10 08:37:47 +08:00
|
|
|
|
<div class="layui-progress layui-progress-big" lay-showPercent="yes" lay-filter="demo">
|
|
|
|
|
<div class="layui-progress-bar" lay-percent=""></div>
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
|
2017-08-21 08:51:13 +08:00
|
|
|
|
<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>
|
2021-05-08 06:31:19 +08:00
|
|
|
|
<th>上传进度</th>
|
2017-08-21 08:51:13 +08:00
|
|
|
|
<th>操作</th>
|
|
|
|
|
</thead>
|
|
|
|
|
<tbody id="demoList"></tbody>
|
|
|
|
|
</table>
|
|
|
|
|
</div>
|
|
|
|
|
<button type="button" class="layui-btn" id="testListAction">开始上传</button>
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
<hr>
|
|
|
|
|
|
2021-05-08 06:31:19 +08:00
|
|
|
|
<div class="layui-btn-container">
|
|
|
|
|
<button type="button" class="layui-btn test333" lay-data="{size:10,url:'a'}" id="test3"><i class="layui-icon"></i>上传文件</button>
|
|
|
|
|
<button type="button" class="layui-btn layui-btn-primary test333" lay-data="{size:20,url:'b'}" id="test33"><i class="layui-icon"></i>换个样式</button>
|
2017-08-21 08:51:13 +08:00
|
|
|
|
|
2021-05-08 06:31:19 +08:00
|
|
|
|
<button type="button" class="layui-btn" id="test4"><i class="layui-icon"></i>上传视频</button>
|
|
|
|
|
<button type="button" class="layui-btn" id="test5"><i class="layui-icon"></i>上传音频</button>
|
|
|
|
|
</div>
|
2017-08-21 08:51:13 +08:00
|
|
|
|
|
|
|
|
|
<hr>
|
|
|
|
|
|
2021-05-08 06:31:19 +08:00
|
|
|
|
<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>
|
2017-08-30 17:10:33 +08:00
|
|
|
|
|
|
|
|
|
<hr>
|
|
|
|
|
|
2021-05-08 06:31:19 +08:00
|
|
|
|
<div class="layui-btn-container">
|
2017-08-21 08:51:13 +08:00
|
|
|
|
<button type="button" class="layui-btn layui-btn-normal" id="test6">选择文件</button>
|
|
|
|
|
<button type="button" class="layui-btn" id="test7">开始上传</button>
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
<hr><br>
|
|
|
|
|
|
2021-05-08 06:31:19 +08:00
|
|
|
|
<div class="layui-upload-drag" id="test8">
|
2017-08-21 08:51:13 +08:00
|
|
|
|
<i class="layui-icon"></i>
|
|
|
|
|
<p>点击上传,或将文件拖拽到此处</p>
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
<hr><br>
|
|
|
|
|
|
2021-05-08 06:31:19 +08:00
|
|
|
|
绑定原始文件域:<input type="file" name="file" id="test9">
|
2017-08-21 08:51:13 +08:00
|
|
|
|
|
2022-06-05 23:55:31 +08:00
|
|
|
|
<script src="../src/layui.js" src1="https://cdn.staticfile.org/layui/2.6.8/layui.js"></script>
|
2017-08-21 08:51:13 +08:00
|
|
|
|
<script>
|
2019-09-10 08:37:47 +08:00
|
|
|
|
layui.use(['upload', 'element'], function(){
|
2017-08-21 08:51:13 +08:00
|
|
|
|
var $ = layui.jquery
|
2019-09-10 08:37:47 +08:00
|
|
|
|
,upload = layui.upload
|
|
|
|
|
,element = layui.element;
|
2017-08-21 08:51:13 +08:00
|
|
|
|
|
|
|
|
|
var uploadInst = upload.render({
|
|
|
|
|
elem: '#test1'
|
2022-05-27 07:28:25 +08:00
|
|
|
|
,url: 'https://httpbin.org/post'
|
2019-09-10 08:37:47 +08:00
|
|
|
|
//,size: 2000 //限制文件大小,单位 KB
|
2018-08-18 20:46:14 +08:00
|
|
|
|
//,accept: 'file'
|
|
|
|
|
,method: 'get'
|
2018-04-03 06:41:11 +08:00
|
|
|
|
,fileAccept: 'image/*'
|
2017-12-07 15:48:45 +08:00
|
|
|
|
,exts: "jpg|png|gif|bmp|jpeg|pdf"
|
2018-05-28 18:50:37 +08:00
|
|
|
|
,data: { //额外参数
|
|
|
|
|
a: 1
|
|
|
|
|
,b: function(){
|
|
|
|
|
return 2
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-05-27 07:28:25 +08:00
|
|
|
|
,choose: function(obj){
|
|
|
|
|
console.log('choose', obj);
|
2018-05-28 18:50:37 +08:00
|
|
|
|
}
|
2017-08-21 08:51:13 +08:00
|
|
|
|
,before: function(obj){
|
|
|
|
|
//预读本地文件示例,不支持ie8
|
|
|
|
|
obj.preview(function(index, file, result){
|
|
|
|
|
$('#demo1').attr('src', result); //图片链接(base64)
|
|
|
|
|
});
|
2021-05-18 03:58:53 +08:00
|
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
layer.msg('不允许上传')
|
|
|
|
|
return false;
|
2017-08-21 08:51:13 +08:00
|
|
|
|
}
|
2022-06-05 23:55:31 +08:00
|
|
|
|
,done: function(res, index){
|
2019-05-31 14:36:29 +08:00
|
|
|
|
|
2017-08-21 08:51:13 +08:00
|
|
|
|
//如果上传失败
|
|
|
|
|
if(res.code > 0){
|
|
|
|
|
return layer.msg('上传失败');
|
|
|
|
|
}
|
|
|
|
|
//上传成功
|
2022-06-05 23:55:31 +08:00
|
|
|
|
console.log(res, index);
|
2017-08-21 08:51:13 +08:00
|
|
|
|
}
|
2022-06-05 23:55:31 +08:00
|
|
|
|
,error: function(index, upload){
|
2019-05-31 14:36:29 +08:00
|
|
|
|
this.item.html('重选上传');
|
|
|
|
|
|
2017-08-21 08:51:13 +08:00
|
|
|
|
//演示失败状态,并实现重传
|
|
|
|
|
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();
|
|
|
|
|
});
|
2019-09-10 08:37:47 +08:00
|
|
|
|
|
|
|
|
|
element.progress('demo', '0%');
|
|
|
|
|
}
|
2021-05-08 06:31:19 +08:00
|
|
|
|
,progress: function(n, elem, res, index){
|
2022-06-05 23:55:31 +08:00
|
|
|
|
console.log(n + '%', elem, res, index); //获取进度百分比
|
2019-09-10 08:37:47 +08:00
|
|
|
|
element.progress('demo', n + '%'); //可配合 layui 进度条元素使用
|
2017-08-21 08:51:13 +08:00
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
2019-05-31 14:36:29 +08:00
|
|
|
|
//重置上述 upload 实例
|
|
|
|
|
uploadInst.reload({
|
|
|
|
|
name:'avatar'
|
2019-06-02 23:14:47 +08:00
|
|
|
|
,accept: 'images' //只允许上传图片
|
|
|
|
|
,acceptMime: 'image/*' //只筛选图片
|
2019-09-10 08:37:47 +08:00
|
|
|
|
//,size: 2
|
2019-05-31 14:36:29 +08:00
|
|
|
|
});
|
|
|
|
|
|
2022-06-05 23:55:31 +08:00
|
|
|
|
// 演示多图片上传
|
2017-08-21 08:51:13 +08:00
|
|
|
|
upload.render({
|
|
|
|
|
elem: '#test2'
|
2022-06-05 23:55:31 +08:00
|
|
|
|
,url: 'https://httpbin.org/post'
|
2017-08-21 08:51:13 +08:00
|
|
|
|
,multiple: true
|
2022-06-05 23:55:31 +08:00
|
|
|
|
,accept: 'images'
|
|
|
|
|
,number: 3 //同时上传的数量
|
2017-09-01 07:52:28 +08:00
|
|
|
|
,size: 1024
|
2017-08-21 08:51:13 +08:00
|
|
|
|
,before: function(obj){
|
|
|
|
|
//预读本地文件示例,不支持ie8
|
|
|
|
|
obj.preview(function(index, file, result){
|
|
|
|
|
$('#demo2').append('<img src="'+ result +'" alt="'+ file.name +'" class="layui-upload-img">')
|
|
|
|
|
});
|
2022-06-05 23:55:31 +08:00
|
|
|
|
|
|
|
|
|
//this.files = obj.pushFile();
|
2017-08-21 08:51:13 +08:00
|
|
|
|
}
|
2022-06-05 23:55:31 +08:00
|
|
|
|
,done: function(res, index){
|
2017-08-21 08:51:13 +08:00
|
|
|
|
//上传完毕
|
2022-06-05 23:55:31 +08:00
|
|
|
|
//console.log(this.files)
|
2017-08-21 08:51:13 +08:00
|
|
|
|
}
|
2017-11-02 22:57:10 +08:00
|
|
|
|
,allDone: function(obj){
|
|
|
|
|
console.log(obj)
|
|
|
|
|
}
|
2017-08-21 08:51:13 +08:00
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
//演示多文件列表
|
|
|
|
|
var demoListView = $('#demoList');
|
|
|
|
|
var uploadListIns = upload.render({
|
|
|
|
|
elem: '#testList'
|
2022-06-05 23:55:31 +08:00
|
|
|
|
,url: 'https://httpbin.org/post'
|
2017-08-21 08:51:13 +08:00
|
|
|
|
,accept: 'file'
|
|
|
|
|
,multiple: true
|
2017-12-07 15:48:45 +08:00
|
|
|
|
,number: 3
|
2017-08-21 08:51:13 +08:00
|
|
|
|
,auto: false
|
|
|
|
|
,bindAction: '#testListAction'
|
|
|
|
|
,choose: function(obj){
|
2017-12-07 21:48:54 +08:00
|
|
|
|
var files = this.files = obj.pushFile(); //将每次选择的文件追加到文件队列
|
2017-08-21 08:51:13 +08:00
|
|
|
|
//读取本地文件
|
2022-06-05 23:55:31 +08:00
|
|
|
|
obj.preview(function(index, file, result){
|
2017-08-21 08:51:13 +08:00
|
|
|
|
var tr = $(['<tr id="upload-'+ index +'">'
|
|
|
|
|
,'<td>'+ file.name +'</td>'
|
|
|
|
|
,'<td>'+ (file.size/1014).toFixed(1) +'kb</td>'
|
2021-05-08 06:31:19 +08:00
|
|
|
|
,'<td><div class="layui-progress" lay-filter="progress-'+ index +'"><div class="layui-progress-bar" lay-percent=""></div></div></td>'
|
2017-08-21 08:51:13 +08:00
|
|
|
|
,'<td>'
|
2021-05-08 06:31:19 +08:00
|
|
|
|
,'<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>'
|
2017-08-21 08:51:13 +08:00
|
|
|
|
,'</td>'
|
|
|
|
|
,'</tr>'].join(''));
|
|
|
|
|
|
|
|
|
|
//单个重传
|
|
|
|
|
tr.find('.demo-reload').on('click', function(){
|
|
|
|
|
obj.upload(index, file);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
//删除
|
|
|
|
|
tr.find('.demo-delete').on('click', function(){
|
|
|
|
|
delete files[index]; //删除对应的文件
|
|
|
|
|
tr.remove();
|
2017-12-08 12:13:05 +08:00
|
|
|
|
uploadListIns.config.elem.next()[0].value = ''; //清空 input file 值,以免删除后出现同名文件不可选
|
2017-08-21 08:51:13 +08:00
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
demoListView.append(tr);
|
2021-05-08 06:31:19 +08:00
|
|
|
|
|
|
|
|
|
element.render('progress');
|
2017-08-21 08:51:13 +08:00
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
,done: function(res, index, upload){
|
2021-05-08 06:31:19 +08:00
|
|
|
|
//if(res.code == 0){ //上传成功
|
2017-08-21 08:51:13 +08:00
|
|
|
|
var tr = demoListView.find('tr#upload-'+ index)
|
|
|
|
|
,tds = tr.children();
|
|
|
|
|
tds.eq(3).html(''); //清空操作
|
2017-12-07 21:48:54 +08:00
|
|
|
|
delete this.files[index]; //删除文件队列已经上传成功的文件
|
2017-08-21 08:51:13 +08:00
|
|
|
|
return;
|
2021-05-08 06:31:19 +08:00
|
|
|
|
//}
|
2017-08-21 08:51:13 +08:00
|
|
|
|
this.error(index, upload);
|
|
|
|
|
}
|
2017-12-07 15:48:45 +08:00
|
|
|
|
,allDone: function(obj){
|
|
|
|
|
console.log(obj)
|
|
|
|
|
}
|
2017-08-21 08:51:13 +08:00
|
|
|
|
,error: function(index, upload){
|
|
|
|
|
var tr = demoListView.find('tr#upload-'+ index)
|
|
|
|
|
,tds = tr.children();
|
|
|
|
|
tds.eq(3).find('.demo-reload').removeClass('layui-hide'); //显示重传
|
|
|
|
|
}
|
2021-05-08 06:31:19 +08:00
|
|
|
|
,progress: function(n, elem, e, index){
|
|
|
|
|
console.log(n);
|
|
|
|
|
element.progress('progress-'+ index, n + '%'); //进度条
|
|
|
|
|
}
|
2017-08-21 08:51:13 +08:00
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
upload.render({
|
2017-08-30 17:10:33 +08:00
|
|
|
|
elem: '.test333'
|
|
|
|
|
,url: 'a'
|
2017-08-21 08:51:13 +08:00
|
|
|
|
,accept: 'file'
|
2017-08-30 17:10:33 +08:00
|
|
|
|
,before: function(obj){
|
|
|
|
|
console.log(this.item);
|
|
|
|
|
}
|
2017-08-21 08:51:13 +08:00
|
|
|
|
,done: function(res){
|
|
|
|
|
console.log(res)
|
|
|
|
|
}
|
|
|
|
|
});
|
2017-08-30 17:10:33 +08:00
|
|
|
|
|
|
|
|
|
upload.render({
|
|
|
|
|
elem: '.testm'
|
|
|
|
|
,done: function(res, index, upload){
|
|
|
|
|
//获取当前触发上传的元素,一般用于 elem 绑定 class 的情况,注意:此乃 layui 2.1.0 新增
|
|
|
|
|
var item = this.item;
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
/*
|
2017-08-21 08:51:13 +08:00
|
|
|
|
upload.render({
|
|
|
|
|
elem: '#test33'
|
|
|
|
|
,url: ''
|
|
|
|
|
,accept: 'file'
|
|
|
|
|
,done: function(res){
|
|
|
|
|
console.log(res)
|
|
|
|
|
}
|
2017-08-30 17:10:33 +08:00
|
|
|
|
});*/
|
2017-08-21 08:51:13 +08:00
|
|
|
|
|
|
|
|
|
upload.render({
|
|
|
|
|
elem: '#test4'
|
|
|
|
|
,url: ''
|
|
|
|
|
,accept: 'video'
|
|
|
|
|
,done: function(res){
|
|
|
|
|
console.log(res)
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
upload.render({
|
|
|
|
|
elem: '#test5'
|
|
|
|
|
,url: ''
|
|
|
|
|
,accept: 'audio'
|
|
|
|
|
,done: function(res){
|
|
|
|
|
console.log(res)
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
2018-05-28 18:50:37 +08:00
|
|
|
|
//手动上传
|
2017-08-21 08:51:13 +08:00
|
|
|
|
upload.render({
|
|
|
|
|
elem: '#test6'
|
|
|
|
|
,url: ''
|
|
|
|
|
,auto: false
|
|
|
|
|
//,multiple: true
|
|
|
|
|
,bindAction: '#test7'
|
2022-06-05 23:55:31 +08:00
|
|
|
|
,choose: function(obj){
|
2018-05-28 18:50:37 +08:00
|
|
|
|
var that = this;
|
|
|
|
|
obj.preview(function(index, file){
|
2021-05-08 06:31:19 +08:00
|
|
|
|
console.log(file.name);
|
2022-06-05 23:55:31 +08:00
|
|
|
|
//obj.resetFile(index, file, '123.jpg');
|
2018-05-28 18:50:37 +08:00
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
,before: function(){
|
|
|
|
|
console.log(345);
|
|
|
|
|
}
|
2017-08-21 08:51:13 +08:00
|
|
|
|
,done: function(res){
|
2018-05-28 18:50:37 +08:00
|
|
|
|
console.log(res);
|
2017-08-21 08:51:13 +08:00
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
upload.render({
|
|
|
|
|
elem: '#test8'
|
2022-06-05 23:55:31 +08:00
|
|
|
|
,url: 'https://httpbin.org/post'
|
2017-08-21 08:51:13 +08:00
|
|
|
|
,done: function(res){
|
2018-05-28 18:50:37 +08:00
|
|
|
|
console.log(res);
|
2017-08-21 08:51:13 +08:00
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
upload.render({
|
|
|
|
|
elem: '#test9'
|
2021-05-08 06:31:19 +08:00
|
|
|
|
,url: ''
|
2017-08-21 08:51:13 +08:00
|
|
|
|
,done: function(res){
|
2018-05-28 18:50:37 +08:00
|
|
|
|
console.log(res);
|
2017-08-21 08:51:13 +08:00
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
2021-05-08 06:31:19 +08:00
|
|
|
|
|
|
|
|
|
|
2017-08-21 08:51:13 +08:00
|
|
|
|
});
|
|
|
|
|
</script>
|
|
|
|
|
</body>
|
|
|
|
|
</html>
|