HTML代码
<div class=" layui-fluid layui-bg-white"> <fieldset class="layui-elem-field layui-field-title" style="margin-top: 30px;"> <legend>拖拽上传</legend> </fieldset> <div class="layui-upload-drag" id="demoimport"> <i class="layui-icon"></i> <p>点击上传,或将文件拖拽到此处</p> </div> <fieldset class="layui-elem-field layui-field-title" style="margin-top: 30px;"> <legend>为节省服务器开销,导入文件均为临时文件。</legend> </fieldset> </div>
JS代码
layui.use('upload', function () { var $ = layui.jquery , upload = layui.upload; //拖拽上传 upload.render({ elem: '#demoimport' , url: '?m=Demo&a=uploadFile' , accept: 'file' //普通文件 , exts: 'xls|xlsx' //只允许上传压缩文件 , done: function (res) { //如果上传失败 if (res.code == 0) { return layer.msg('上传失败,' + res.err); } //上传成功 if (res.code > 0) { layer.alert(res.err, {icon: 6}, function () { var index = parent.layer.getFrameIndex(window.name); parent.layer.close(index); window.parent.location.reload(); }); } } }); });
PHP后台参数获取
//获取表格的大小,限制上传表格的大小5M $file_size = $_FILES['file']['size']; if ($file_size > 5 * 1024 * 1024) { $res['code'] = '0'; $res['err'] = '只能上传小于5M大小的文件!'; die(json_encode($res)); } //限制上传表格类型 $file_type = $_FILES['file']['type']; if ($file_type != 'application/vnd.ms-excel') { $res['code'] = '0'; $res['err'] = '上传失败,只能上传excel2003的xls格式!'; die(json_encode($res)); }
Done!