MICUU
微资讯
声卡
创新2024全套教程
5.1声卡全套
创新声卡驱动
板载声卡调试全套教程
Sam机架安装
Sam机架
音效助手
专题
文档
技术文档汇总
站内文档
更多
软件
更新日志
关于
装机必备
Gramos模板
光年后台模板
Betube模板
美图
友情链接
站内导航
关于
搜索
退出登录
登录
原创
tp5中封装上传组件教程-基于layui,适用于所有tp版本【多图上传版本】
2019-12-12
28.29w热度
##### 基于tp5的上传封装适合一个页面需要上传多个图片的情况 ###### 1:在common.php中添加如下防范 ```` function uploader($input_name="img",$img=""){ $url = url('business/Upload/upload'); if($img){ //input框的状态 $input_str = '<input type="hidden" value="'.$img.'" name="'.$input_name.'">'; //预览图 $img_str = '<img id="kzl_preview_img'.$input_name.'" src="'.$img.'">'; //预览图中关闭按钮的状态 $clode_status = "block"; }else{ $input_str = '<input type="hidden" name="'.$input_name.'">'; $img_str = '<img id="kzl_preview_img'.$input_name.'" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAQIAAACHCAYAAAD9RVePAAAACXBIWXMAAAsTAAALEwEAmpwYAAAAIGNIUk0AAHolAACAgwAA+f8AAIDpAAB1MAAA6mAAADqYAAAXb5JfxUYAAAs+SURBVHja7N1fSJv3Hsfx76ymJEuasslGfIpkhLRhm8hDl0mu9BgY0otMaNnFSNka6MUJuDZMTij2srnw4BA3yC4GVrEro3ih2c068JzeiWQQRg+SMcJcMUq1F6aJBjWVc1F/kjm1+WNW175fVwn5//A8n+f3+/5+vyeviMg7AuClVscmAEAQACAIABAEAAgCAAQBAIIAAEEAgCAAQBAAIAgAEAQACAIABAEAggAAQQCAIABAEAAgCAAQBAAIAgAEAQCCAABBAIAgAEAQACAIABAEAAgCAAQBAIIAAEEAgCAAQBAAIAgAEAQACAIABAEAggAAQQCAIABAEAAgCAAQBAAIAgAEAQCCAABBAIAgAEAQACAIABAEAAiC2tJ13cJWwIuonk1Qmkgk0u10Om9kMpmJy5cvX2eL1E4oFHKLiKRSqYVYLJYu5TWjo6M3RUTm5+c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AQBAAIAgAEAQCCAABBAIAgAEAQACAIABAEAAgCAAQBAIIAAEEAgCAAQBAAIAgAEAQACAIABAEAggAAQQCAIABAEAAo2/8HALHK0JG14BegAAAAAElFTkSuQmCC">'; $clode_status = "none"; } $uploader_html = <<<EOT <style> #kzl_upload{$input_name} #kzl_uploader{$input_name}{float: left;} #kzl_upload{$input_name} #kzl_preview{$input_name}{float:left; margin-left: 15px; max-width: 258px; max-height: 135px;} #kzl_upload{$input_name} #kzl_close_btn{$input_name} .icon{font-size: 30px; color: red; position: absolute; margin-left: -15px; margin-top: -15px;} #kzl_upload{$input_name} #kzl_preview{$input_name} img{max-width: 258px; max-height: 135px;} </style> <div id="kzl_upload{$input_name}"> {$input_str} <div class="layui-upload-drag" id="kzl_uploader{$input_name}" > <i class="layui-icon">?</i> <p>点击上传,或将文件拖拽到此处</p> </div> <div id="kzl_preview{$input_name}"> {$img_str} </div> <div id="kzl_close_btn{$input_name}" style="display: {$clode_status}"> <i onclick="re_upload{$input_name}()" class="layui-icon layui-icon-close-fill icon">ဇ</i> </div> </div> <script> layui.use('upload', function(){ var upload = layui.upload; var url = "{$url}"; //执行实例 var uploadInst = upload.render({ elem: '#kzl_uploader{$input_name}' //绑定元素 ,url: url //上传接口 ,before: function(obj){ //obj参数包含的信息,跟 choose回调完全一致,可参见上文。 layer.load(); //上传loading } ,done: function(res){ //上传完毕回调 layer.closeAll(); //关闭loading if(res.code == 1 ||res.code ==200){ $("#kzl_close_btn{$input_name}").css("display","block"); var img = res.data; $("#kzl_preview_img{$input_name}").attr("src",img); $("input[name={$input_name}]").val(img); layer.msg(res.msg,{icon:1}); }else { layer.msg(res.msg,{icon:0}); } } ,error: function(){ //请求异常回调 layer.msg("上传出错,稍后重试",{icon:0}); } }); }); //重新上传 function re_upload{$input_name}() { $("#kzl_preview_img{$input_name}").attr("src","data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAQIAAACHCAYAAAD9RVePAAAACXBIWXMAAAsTAAALEwEAmpwYA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ftvaWnx7z5zF1MLo0SezoisRR3k9OnTcv78+Z37x44d23dFZSQS6W5sbLwo8nRI9Pvvv/+aPY0gONKam5vDzc3NNXt/n8+ndXd3/6t4Yc+jR4/GSrmY6fLy8qTNZguazWbv7du3725ubv7pYifF/7GQyWQmnncdZPdl0Obn56MsVCIIjryDrvlfioNmKOq6bvn444/H1YFa7vr+kZGRsZ6enjNms9mrVgQe9Dui0Wh/LX7jPt2RPf/XIJFIpDs7O5MGg8GVy+WmDlqnAYLguVPDWXtddagco6Oj6jJm2T0OiuzMzMxnHo/n5qNHj8a++eabslYxJhKJbCAQuKIq/Ps971nDewf9r0Gl9vtfg0Qikf3iiy8uXb169ctq6x+qQFluoRIsOjqSdF23UCwDQQDgL1XHJgBAEAAgCAAQBAAIAgAEAQCCAABBAIAgAEAQACAIABAEAAgCAAQBAIIAAEEAgCAAQBAAIAgAEAQACAIABAEAggAAQQCAIABAEAAgCAAQBAAIAgAEAQCCAABBAIAgAEAQACAIABAEAAgCAAQBAIIAAEEAgCAAQBAAIAgAEAQACAIABAEAggAAQQCAIABAEAAo2/8HALHK0JG14BegAAAAAElFTkSuQmCC"); $("#kzl_close_btn{$input_name}").css("display","none"); } </script> EOT; echo $uploader_html; } ```` ###### 2:上传图片处理 ```` public function upload() { $file = request()->file('file'); // 移动到框架应用根目录/public/uploads/ 目录下 if($file){ $info = $file->validate(['size'=>3140000,'ext'=>'png,jpg,jpeg,gif'])->move(ROOT_PATH . 'public' . DS . 'uploads'); if($info){ $path = Request::domain().'/public/uploads/'.$info->getSaveName(); $data['code'] = 1; $data['msg'] = "上传成功"; $data['data'] = $path; echo json_encode($data);die; }else{ // 上传失败获取错误信息 echo $file->getError(); } } }</pre> ```` ###### 3:引用 ```` {:uploader('agreement_img','')} {:uploader('agreement_img','http://baidu.com/img1.img')} ````
注:原创不易,转载请注明出处(
https://micuu.com/new/394.html
),本站所有资源来源于网络收集,如有侵权请联系QQ245557979进行清除。
最后修改与 2022-02-22
上一篇:
mac Anaconda 打开pyqt,将ui文件转化为py文件
下一篇:
男生买衣服这样搜索摆平,教男生穿衣打扮
留言反馈
请先登录
问题反馈渠道,如有软件无法下载或者其他问题可反馈。【由于某种原因,目前留言不展示】
用户需要登录后才能留言反馈
立即留言
珍藏视频
10分钟高效燃脂
30天高效瘦脸操
5分钟缓解颈椎操
友人
微博
全民K歌
唱吧
今日头条
悠悠网
科技小锅盖
彼岸桌面
阮一峰
laravel社区
V2ex
掘金
更多>