温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

怎么在ThinkPHP5中使用Layui实现图片上传加预览功能

发布时间:2021-05-22 16:33:36 来源:亿速云 阅读:211 作者:Leah 栏目:开发技术

怎么在ThinkPHP5中使用Layui实现图片上传加预览功能?相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。

html代码

<div class="layui-upload">
 <button type="button" class="layui-btn" id="cover">上传封面</button>
</div> 
<div class="layui-input-inline">
 <img id="preview" width="200px" height="200px">
</div>

js代码

var uploadInst = upload.render({
  elem:'#cover'
  ,url:'addCourse'
  ,accept:'file' // 允许上传的文件类型
  ,auto:true // 自动上传
  ,before:function (obj) {
   console.log(obj);
   // 预览
   obj.preview(function(index,file,result) {
    // console.log(file.name); //图片名字
    // console.log(file.type); //图片格式
    // console.log(file.size); //图片大小
    // console.log(result); //图片地址
    $('#preview').attr('src',result); //图片链接 base64
   });
   // layer.load();
  }
  // 上传成功回调
  ,done:function(res) {
   // console.log(upload);
   console.log(res);
  }
  // 上传失败回调
  ,error:function(index,upload) {
   // 上传失败
  }
 });

php接口

$file = request()->file('file');
 // 移动到框架应用根目录/public/uploads/ 目录下
 $info = $file->move('public/upload/');
 if ($info) {
  $path = 'public/upload/'.$info->getSaveName();
  return return_succ($path);
 }

thinkphp是什么

thinkphp属于一种免费的开发框架,能够用于开发前端网页,最早thinkphp是为了简化开发而产生的,thinkphp同时也是遵循Apache2协议,最初是从Struts演变过来,也把国外一些好的框架模式进行利用,使用面向对象的开发结构,兼容了很多标签库等模式,它能够更方便和快捷的开发和部署应用,当然不仅仅是企业级应用,任何php应用开发都可以从thinkphp的简单、兼容和快速的特性中受益。

看完上述内容,你们掌握怎么在ThinkPHP5中使用Layui实现图片上传加预览功能的方法了吗?如果还想学到更多技能或想了解更多相关内容,欢迎关注亿速云行业资讯频道,感谢各位的阅读!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI