ThinkPHP搭建cms-图片上传插件封装
/** * 图片上传插件封装 * @param int $num 图片上传数量 * @return string * @author wusn <message@jswusn.com> */ function img_upload($num=5){ $str=<<<eof <script type='text/javascript'> eof; $str.="$('#thumb').wrap('<form id="myupload" action="".U('Common/upload')."" method="post" enctype="multipart/form-data"></form>'); "; $str.=<<<eof $('#filePicker').on('click',function(){ $('#file').click(); }); $('#file').change(function(){ if($("#file").val() == "") return; var btn=$('#filePicker'); $("#myupload").ajaxSubmit({ dataType: 'json', beforeSend: function() { $('#litpic_show').empty(); btn.html("上传中..."); }, success: function(data) { console.log(data) if(data.up!=null){ haspic(); var pic=$('#pic'); pic.removeClass('hidden').attr('src',data.path) $('#cat_pic').val(data.up.savepath+data.up.savename); $('#thumb').css('height','auto'); }else{ btn.html("上传失败"); } }, error:function(xhr){ btn.html("上传失败"); litpic_tip.html(xhr); } }); }) function haspic(){ var thumb=$('#thumb'); $('#btnfile').addClass('hidden') thumb.append("<span id='del'>删除</span>") thumb.hover(function(){ $('#del').show() },function(){ $('#del').hide() }) } //删除 $("body").on('click','#del',function(){ $('#thumb').removeAttr("style"); $('#btnfile').removeClass('hidden'); $('#filePicker').html('点击上传图片') $('#pic').addClass('hidden').attr('src',''); $('#cat_pic').val(''); $(this).remove(); }); </script> eof; return $str; }
以上代码由(Ldcms)项目团队构思,苏州网站建设整理!