JS示例:
<script type="text/javascript"> layui.use(['form', 'layedit','element', 'laydate','upload'], function(){ var form = layui.form; var layer = layui.layer; var layedit = layui.layedit; var laydate = layui.laysdate; // 上传 var upload = layui.upload; var uploadInst1 = upload.render({ elem: '#uploadImgBut1', //绑定元素 url: "{:U('Admin/Upload/uploadmedia')}",//上传接口 accept: 'video', done: function(res){ if(res['state'] ==1){ layer.msg(res['message']); $("#uploadImg1").attr('src',res['path']).show(); $("input#uploadImgSrc1").val(res['path']); }; } }); }); </script>
PHP示例:
public function uploadmedia(){ $upload = new ThinkUpload();// 实例化上传类 $mediaSize = intval(CP('MEDIASIZE')); $mediaType = CP('MEDIATYPE'); $mediaSize = !empty($mediaSize) ? $mediaSize : 3145728; $mediaType = !empty($mediaType) ? explode(',',$mediaType) : array('mp3','mp4'); $upload->maxSize = $mediaSize;// 设置附件上传大小 $upload->exts = $mediaType;// 设置附件上传类型 $upload->rootPath = "./Uploads/Video/"; // 设置附件上传根目录 $upload->savePath = ''; // 设置附件上传(子)目录 $dataArr = array(); $dataArr['code'] = 1; $dataArr['msg'] = '上传成功'; $dataArr['data']['src'] = ""; $dataArr['data']['title'] = "错误"; // 上传文件 $info = $upload->upload(); if(!$info){ $dataArr['code'] = 0; $dataArr['msg'] = '上传失败'; }; $path = "/Upload/Video/".$info['file']['savepath'].$info['file']['savename']; $dataArr['data']['src'] = $path; $dataArr['data']['title'] = "成功"; exit(json_encode($dataArr)); }