layui文件上传demo
时间: 2023-08-21 08:03:40 浏览: 180
文件上传demo
以下是一个简单的 layui 文件上传的示例代码:
HTML 代码:
```html
<div class="layui-upload">
<button type="button" class="layui-btn" id="test-upload-normal">选择文件</button>
<div class="layui-upload-list">
<table class="layui-table">
<thead>
<tr>
<th>文件名</th>
<th>大小</th>
<th>状态</th>
<th>操作</th>
</tr>
</thead>
<tbody id="demoList"></tbody>
</table>
</div>
<button type="button" class="layui-btn" id="test-upload-reload">重传</button>
<button type="button" class="layui-btn layui-btn-danger" id="test-upload-delete">删除</button>
</div>
```
JavaScript 代码:
```javascript
layui.use('upload', function(){
var $ = layui.jquery
,upload = layui.upload;
//普通图片上传
var uploadInst = upload.render({
elem: '#test-upload-normal'
,url: '/upload/'
,accept: 'file' //允许上传的文件类型
,done: function(res){
//上传完毕回调
console.log(res);
if(res.code == 0){
var demoText = $('#demoText');
demoText.html('<span style="color: #5FB878;">上传成功</span>');
$('#demoList').append('<tr><td>'+ res.filename +'</td><td>'+ (res.size/1024).toFixed(1) +'kb</td><td>上传成功</td><td><button class="layui-btn layui-btn-xs layui-btn-danger demo-delete">删除</button></td></tr>');
}else{
var demoText = $('#demoText');
demoText.html('<span style="color: #FF5722;">上传失败</span> ' + res.msg);
}
}
,error: function(){
//请求异常回调
var demoText = $('#demoText');
demoText.html('<span style="color: #FF5722;">上传失败</span> 服务器异常,请稍后重试');
}
});
//文件列表操作
var demoListView = $('#demoList')
,uploadListIns = upload.render({
elem: '#test-list'
,url: '/upload/'
,accept: 'file'
,multiple: true
,auto: false
,bindAction: '#testListAction'
,choose: function(obj){
var files = obj.pushFile();
//预读本地文件示例,不支持ie8
obj.preview(function(index, file, result){
console.log(index, file, result);
$('#demoList').append('<tr><td>'+ file.name +'</td><td>'+ (file.size/1024).toFixed(1) +'kb</td><td>等待上传</td><td><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></td></tr>')
});
}
,done: function(res, index, upload){
if(res.code == 0){ //上传成功
var tr = demoListView.find('tr').eq(index)
,tds = tr.children();
tds.eq(2).html('<span style="color: #5FB878;">上传成功</span>');
tds.eq(3).html('<button class="layui-btn layui-btn-xs layui-btn-danger demo-delete">删除</button>'); //清空操作
return delete this.files[index]; //删除文件队列已经上传成功的文件
}
this.error(index, upload);
}
,error: function(index, upload){
var tr = demoListView.find('tr').eq(index)
,tds = tr.children();
tds.eq(2).html('<span style="color: #FF5722;">上传失败</span>');
tds.eq(3).find('.demo-reload').removeClass('layui-hide'); //显示重传
}
});
//删除文件
$('#demoList').on('click', '.demo-delete', function(){
var tr = $(this).parents('tr')
,index = tr.index();
//删除对应的文件
uploadListIns.config.elem.next()[0].value = '';
uploadListIns.config.files.splice(index, 1);
tr.remove();
});
//重传文件
$('#demoList').on('click', '.demo-reload', function(){
var tr = $(this).parents('tr')
,index = tr.index();
//删除对应的文件
uploadListIns.config.files.splice(index, 1);
tr.remove();
uploadListIns.upload();
});
});
```
这个示例代码实现了单文件上传和多文件上传的功能,并且可以实现文件的删除和重传操作。你可以根据自己的需求进行修改和扩展。
阅读全文