简单示例:

<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/layuiupload')}",//上传接口
done: function(res){
if(res['state'] ==1){
layer.msg(res['message']);
$("#uploadImg1").attr('src',res['path']).show();
$("input#uploadImgSrc1").val(res['path']);
};
}
});
});
</script>

后台代码:

public function layuiupload(){
$upload = new \Think\Upload();// 实例化上传类
$imgSize = intval(CP('IMGSIZE'));
$imgType = CP('IMGTYPE');
$imgSize = !empty($imgSize) ? $imgSize : 3145728;
$imgType = !empty($imgType) ? explode(',',$imgType) : array('jpg','gif','png','jpeg');
$upload->maxSize = $imgSize;// 设置附件上传大小
$upload->exts = $imgType;// 设置附件上传类型
$upload->rootPath = "./uploads/Picture/"; // 设置附件上传根目录
$upload->savePath = ''; // 设置附件上传(子)目录
$data = array();
$data['state'] = 1;
$data['message'] = '上传成功';
$data['path'] = '';
// 上传文件
$info = $upload->upload();
if(!$info){
$data['state'] = 0;
$data['message'] ='上传失败';
};
$path = "uploads/Picture/".$info['file']['savepath'].$info['file']['savename'];
$data['path'] = $path;
echo json_encode($data);die;
}

上传图片,压缩和裁剪

public function layuiupload(){
$upload = new \Think\Upload();// 实例化上传类
$image = new \Think\Image();
$imgSize = intval(CP('IMGSIZE'));
$imgType = CP('IMGTYPE');
$imgSize = !empty($imgSize) ? $imgSize : 3145728;
$imgType = !empty($imgType) ? explode(',',$imgType) : array('mp4','jpg','gif','png','jpeg');
$upload->thumb = true;
$upload->maxSize = $imgSize;// 设置附件上传大小
$upload->exts = $imgType;// 设置附件上传类型
$upload->rootPath = "./Uploads/Picture/"; // 设置附件上传根目录
$upload->savePath = ''; // 设置附件上传(子)目录
$data = array();
$data['state'] = 1;
$data['message'] = '上传成功';
$data['path'] = '';
// 上传文件
$info = $upload->upload();
if(!$info){
$data['state'] = 0;
$data['message'] ='上传失败';
};
$path = "/Uploads/Picture/".$info['file']['savepath'].$info['file']['savename'];
$image->open(".".$path);
$image->thumb(230, 160,\Think\Image::IMAGE_THUMB_FIXED)->save(".".$path);
$data['path'] = $path;
echo json_encode($data);die;
}

thinkphp结合layui上传图片的更多相关文章

  1. Thinkphp5+Layui上传图片

    ThinkPHP是一个免费开源的,快速.简单的面向对象的轻量级PHP开发框架,是为了敏捷WEB应用开发和简化企业应用开发而诞生的.ThinkPHP从诞生以来一直秉承简洁实用的设计原则,在保持出色的性能 ...

  2. Layui 上传图片到磁盘上 + Tomcat 配置虚拟路径

    Layui 上传图片到磁盘上 + Tomcat 配置虚拟路径 Tomcat 配置虚拟路径 找到 eclipse 中 tomcat 下面的 server.xml 文件,在 Host 标签里面添加 < ...

  3. layui 上传图片 实现过程

    layui.user一个页面只能有一个,写多了会实现js效果 上传图片官方文档有很多功能,但是演示的代码只是一个一个功能演示,如果要综合起来js代码不是简单的拼凑,需要放在指定位置,比如下面的限制文件 ...

  4. Layui上传图片 带接口

    layui.use('upload', function () { var upload = layui.upload; upload.render({ elem: '#LAY_avatarUploa ...

  5. 上传头像,layui上传图片

    layui上传与bootstrap上传相似,只是不需要下插件, layui自带的已够用 先看一下前台界面,这里是用到的上传头像 先点击开始上传,头像上传至服务器中, 返回json添加至form表单中, ...

  6. thinkphp结合layui上传视频

    JS示例: <script type="text/javascript"> layui.use(['form', 'layedit','element', 'layda ...

  7. layui——上传图片,并实现放大预览

    一般上传文件后会返回文件的路径,然后存储到数据库,那么首先实现上传后的放大和删除功能 function uploadSmallPic() { var upload = layui.upload; up ...

  8. layui上传图片接口

    mvc中 前台调用接口 url:"../upload/uploadfiles/" 然后开始接口 代码: string a = ""; try { HttpFil ...

  9. layui 上传图片回显并点击放大实现

    1.页面代码布局 <div class="layui-col-xs12 form-group"> <div class="layui-col-xs6&q ...

随机推荐

  1. dede栏目添加自定义字段方法

    1.首先要进mysql 数据库里添加字段,命名好!比如我下面添加了一个栏目备注字段,当然你字段可以自己新建,找到表dede_arctype(这个是栏目模型的数据库表,这里面我添加的是栏目备注字段cla ...

  2. Eclipse Maven 配置setting.xml 的镜像远程仓库

    1.在.m2中新建settings.xml文件 1.window-->Preferences-->Maven-->User Settings 3.点击open file 编辑将远程仓 ...

  3. Go之go与channel组合使用

    1,等待一个事件 <-ch 将一直阻塞,直到ch被关闭 或者 ch中可以取出值 为止 所以到第17行之后会去执行go后面的func()匿名函数,在里面给ch赋值后(或者close(ch))后,才 ...

  4. PowerDesigner导出word,PowerDesigner把表导出到word,PDM导出word文档

    PowerDesigner导出word,PowerDesigner把表导出到word,PDM导出word文档 >>>>>>>>>>>& ...

  5. 【安全开发】Perl安全编码规范

    多年以来,Perl已经成为用于系统管理和WebCGI开发的功能最强的编程语言之一(几乎可以使用Perl做任何功能的程序).但其扩展应用,即作为Internet上CGI的开发工具,使得它经常成为Web服 ...

  6. NSIS安装vcredist_64.exe

    ; ExecWait ‘vcredist_x86.exe’ # 一般的安装ExecWait ‘”vcredist_x86.exe” /q’ # silent install 静默安装; ExecWai ...

  7. Linux下chkconfig命令详解转载

    chkconfig命令主要用来更新(启动或停止)和查询系统服务的运行级信息.谨记chkconfig不是立即自动禁止或激活一个服务,它只是简单的改变了符号连接. 使用语法:chkconfig [--ad ...

  8. C语言中的正负数及其输出

    在数学中,数字有正负之分.在C语言中也是一样,short.int.long 都可以带上正负号,例如: //负数 ; short a2 = -0x2dc9; //十六进制 //正数 ; ; //八进制 ...

  9. Failure to transfer org.springframework.boot:spring-boot-starter-parent:pom:2.0.1.RELEASE from https://repo.maven.apache.org/maven2 was cached in the local repository, resolution will not be reattempt

    第一次用 Spring Starter Project 创建一个Spring应用时,POM 文件报错: Project build error: Non-resolvable parent POM f ...

  10. Win8设计——现代设计,可使你的应用脱颖而出的元素

    Microsoft 设计准则 Windows 在现代设计方面遥遥领先.它采用了“真实数字”原则并从瑞士风格和交通枢纽的寻路系统中汲取灵感. 阅读详细信息 设计元素 动态磁贴 动态磁贴向你提供了一个独特 ...