设为首页收藏本站

安徽论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 10630|回复: 0

tp5使用layui实现多个图片上传(带附件选择)的方法实例

[复制链接]

63

主题

491

回帖

935

积分

高级会员

Rank: 4

积分
935
发表于 2022-3-26 10:59:29 | 显示全部楼层 |阅读模式
网站内容均来自网络,本站只提供信息平台,如有侵权请联系删除,谢谢!
tp5使用layui实现多个图片上传(带附件选择),如何加载layui在此不详细说明,有需要可以百度

html代码,主要处理都是在jq中,完成方法全部原创,也许不是最简单的,但也能实现效果
  1. {include file="public/header" /}
  2. <body>
  3. <div class="x-nav">
  4.       <span class="layui-breadcrumb">
  5.         <a href="">首页</a>
  6.         <a href="">多选图片列表</a>
  7.         <a>
  8.           <cite>添加多选图片</cite></a>
  9.       </span>
  10.   <a class="layui-btn layui-btn-small" style="line-height:1.6em;margin-top:3px;float:right" href="javascript:location.replace(location.href);" rel="external nofollow"  title="刷新">
  11.     <i class="layui-icon" style="line-height:30px">ဂ</i></a>
  12. </div>
  13. <div class="x-body">
  14.   <div class="top" style="font-size: 14px">添加多选图片</div>
  15.   <div style="width:100%;height: 5px;background-color: #077ee3;margin-top: 5px;margin-bottom: 20px"></div>

  16.     <form  action="" enctype="multipart/form-data" method="post" role="form" onSubmit="return check()">


  17.       <div class="layui-upload">
  18.         <button type="button" class="layui-btn" id="upload_img">多图片上传</button>
  19.         <a class="layui-btn layui-btn-sm layui-btn-normal" onclick="selectImg('选择图片','{:url("selectImg")}','1000','600')" href="javascript:;" rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow" >
  20.         <i class="layui-icon">&#xe60a;</i>选择图片
  21.         </a>
  22.         <blockquote class="layui-elem-quote layui-quote-nm" style="margin-top: 10px;">
  23.           预览图:
  24.           <div class="layui-upload-list" id="demo2"></div>
  25.         </blockquote>
  26.       </div>

  27.       <input  name="url" class="imgInput" type="hidden">
  28.       <!--<button type="button"  class="layui-btn" onclick="test()">-->
  29.         <!--测试-->
  30.       <!--</button>-->
  31.     <div class="layui-form-item">
  32.       <label for="" class="layui-form-label">
  33.       </label>
  34.       <button type="submit" class="layui-btn btnAdd" lay-filter="add" lay-submit="">
  35.         增加
  36.       </button>

  37.     </div>
  38.   </form>
  39. </div>
  40. <style>
  41.   .imgInput{
  42.     width: 600px;
  43.     height: 35px;
  44.   }
  45.   .layui-form-label{
  46.     font-size: 14px;
  47.     width: 100px;
  48.   }

  49.   select{
  50.     width: 500px;
  51.   }
  52.   #pre_img{
  53.     display: none;
  54.     padding: 5px;
  55.     border: 1px solid #999;
  56.   }
  57.   #demo2{
  58.     display: flex;
  59.     display: -webkit-flex;
  60.     /*justify-content: space-between;*/
  61.     flex-direction: row;
  62.     flex-wrap: wrap;
  63.   }
  64.   .img{
  65.     width: 150px;
  66.     height: 150px;
  67.   }
  68.   .btnAdd{
  69.     margin-top: 40px;
  70.   }
  71.   .img_item{
  72.     display: flex;
  73.     flex-direction: column;
  74.     text-align: center;
  75.     margin-right: 20px;
  76.     margin-bottom: 20px;
  77.   }
  78.   .delimg{
  79.     text-align: center;
  80.     line-height: 20px;
  81.     width: 160px;
  82.     height: 20px;
  83.     background-color: red;
  84.     color: white;
  85.     margin-top: 5px;
  86.   }
  87. </style>

  88. <script>
  89.   function check(){
  90.     $('input[name="url"]').val(urlList);
  91.     var str = $('input[name="url"]').val();

  92.     if(str ==''|| str==null || str=='undefined'){
  93.       alert("请选择图片");
  94.       return false;
  95.     }

  96.   }

  97.   function selectImgGo($url,$urlWeb){
  98.     var index = 0;
  99.     if(urlList.length>0){
  100.       index = urlList.length;
  101.     }
  102.       var img = $([
  103.         '<div class="img_item">',
  104.         '<div class="img" style="overflow:hidden;">',
  105.         '<img src="'+ $urlWeb +'" alt="' + $url +'" class="layui-upload-img" style="max-width:150px;"></div>',
  106.         '<div class="delimg" id="delimg" onclick=delImg("'+ index +'")  href="javascript:;" rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  style="cursor:pointer ">删除</div></div>'
  107.       ].join(''));
  108.       $('#demo2').append(img);

  109.     urlList.push($url);
  110.     imgList.push($urlWeb);
  111.   }
  112.   var imgList = [];
  113.   var urlList = [];
  114.   layui.use(['upload','jquery'],function () {
  115.     $ = layui.jquery;
  116.     var upload = layui.upload;
  117.     //多图片上传
  118.     upload.render({
  119.       elem: '#upload_img'
  120.       ,url:"{:url('share/upload_img')}" //上传接口
  121.       ,multiple: true
  122.       ,before: function(obj){
  123.         //预读本地文件示例,不支持ie8
  124.         obj.preview(function(index, file, result){

  125.         })
  126.       }
  127.       ,done: function(res){
  128.         var index0 = 0;
  129.         if(urlList.length>0){
  130.           index0 = urlList.length;
  131.         }
  132.         var img0 = $([
  133.           '<div class="img_item">',
  134.           '<div class="img" style="overflow:hidden;">',
  135.           '<img src="'+ res.msg +'"  class="layui-upload-img" style="max-width:150px;"></div>',
  136.           '<div class="delimg" id="delimg" onclick=delImg("'+ index0 +'")  href="javascript:;" rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  style="cursor:pointer ">删除</div></div>'
  137.         ].join(''));
  138.         $('#demo2').append(img0);
  139.         urlList.push(res.url);
  140.         imgList.push(res.msg);
  141.       }
  142.       ,error: function(){
  143. //        layer.close(layer.msg());//关闭上传提示窗口
  144.         //请求异常回调
  145.       }

  146.     });
  147.   });

  148.   function delImg(index){
  149.     urlList.splice(index,1);
  150.     imgList.splice(index,1);
  151.     $('#demo2').empty();
  152.     for (var i=0;i<imgList.length;i++){
  153.       var img0 = $([
  154.         '<div class="img_item">',
  155.         '<div class="img" style="overflow:hidden;">',
  156.         '<img src="'+ imgList[i] +'"  class="layui-upload-img" style="max-width:150px;"></div>',
  157.         '<div class="delimg" id="delimg" onclick=delImg("'+ i +'")  href="javascript:;" rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  style="cursor:pointer ">删除</div></div>'
  158.       ].join(''));
  159.       $('#demo2').append(img0);
  160.     }
  161.   }

  162.   /*选择图片*/
  163.   function selectImg(title,url,w,h){
  164.     x_admin_show(title,url,w,h);
  165.   }
  166. </script>
  167. </body>
  168. </html>
复制代码
php代码
  1.   //php layui图片上传
  2.     public function upload_img(){
  3.         $file = request()->file('file'); // 获取上传的文件
  4.         if($file==null){
  5.             exit(json_encode(array('code'=>1,'msg'=>'未上传图片')));
  6.         }else{
  7.             //5、对上传文件做出条件限制(类型,大小等)
  8.             $map = [
  9.                 'ext'=>'jpg,png,gif,jpeg',//后辍名
  10.                 'size'=>320000000,//最大3M
  11.             ];
  12.             //6、对上传的文件进行较验,如果合格就进行转移到预定设定好的public/uploads目录下
  13.             //返回保存的文件信息info,包括文件名和大小等数据
  14.             $info = $file->validate($map)->move(ROOT_PATH . 'public/uploads/img');
  15.             //获取图片宽高
  16.             list( $width , $height , $type , $attr ) = getimagesize ($info->getPathName());

  17.             if(is_null($info)){
  18.                 $this->error($info->getError());
  19.             }
  20.             $img = str_replace('\\','/',$info->getSaveName());
  21.             //保存附件
  22.             $annexData['filesize'] = $info->getInfo()['size'];
  23.             $annexData['mimetype'] = $info->getInfo()['type'];
  24.             $annexData['filename'] = $info->getInfo()['name'];
  25.             $annexData['imagewidth'] = $width;
  26.             $annexData['imageheight'] = $height;
  27.             $annexData['type'] = 'img';
  28.             $annexData['url'] = $img;
  29.             AAnnexModel::create($annexData);
  30.             $img = constant("URL")."/uploads/img/".$img;
  31.             exit(json_encode(array('code'=>0,'msg'=>$img,'url'=>$annexData['url'])));
  32.         }

  33.     }

  34. //多选图片添加页面
  35.     public function addImages(){
  36.         if($this->request->isPost()){
  37.             //2、获取提交过来的数据,最后true参数,表示连上传文件一起获取
  38.             $data = $this->request->param(true);
  39.             $res = AImagesModel::create($data);
  40.             if(is_null($res)){
  41.                 $this->error('文件添加失败');
  42.             }else{
  43.                 $this->success('文件添加成功...',url('imagesList'));
  44.             }
  45.             return;
  46.         }
  47.         return $this->fetch('images_add');
  48.     }
复制代码
到此这篇关于tp5使用layui实现多个图片上传(带附件选择)的方法实例的文章就介绍到这了,更多相关tp5 多图片上传内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
                                                        
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
免责声明
1. 本论坛所提供的信息均来自网络,本网站只提供平台服务,所有账号发表的言论与本网站无关。
2. 其他单位或个人在使用、转载或引用本文时,必须事先获得该帖子作者和本人的同意。
3. 本帖部分内容转载自其他媒体,但并不代表本人赞同其观点和对其真实性负责。
4. 如有侵权,请立即联系,本网站将及时删除相关内容。
懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

快速回复 返回顶部 返回列表