刚才有发一个flask+layui上传的方法,现在发布一个tp5.0+layui的上传方法,注意了tp5.0和tp5.1还是有些微差别,这是tp5.0的
控制器代码:
<?php namespace app\admin\controller; class Attchment extends Base{ public function upload(){ $file = request()->file('file'); $uploaddir = 'uploads'; $data = array(); if($file){ $info = $file->move(ROOT_PATH . $uploaddir); if($info){ $data['code'] = 1; $data['msg'] = 'OK'; $data['src'] = '/'.$uploaddir.'/'.$info->getSaveName(); }else{ // 上传失败获取错误信息 $data['code'] = 0; } } echo json_encode($data); } }
前段代码:
<div class="layui-uploadbox"> <div class="layui-qrcode"></div> <input type="hidden" name="qrcode" id="qrcode" class="layui-input" style="width: 170px;"> <button type="button" class="layui-btn" id="upload"><i class="layui-icon"></i>上传二维码</button> </div>
layui上传方法:
var uploadInst = upload.render({ elem: '#upload' //绑定元素 ,url: '{:url('Attchment/upload')}' //上传接口 ,done: function(res){ if(res.code == 1){ $('#qrcode').val(res.src); $('.layui-qrcode').html('<img src="'+res.src+'" />'); }else{ layer.alert('上传出错!'); } } ,error: function(error){ layer.alert('上传出错!'); } });