ThinkPHP搭建cms-图片上传插件封装

jswusn.com PHP 2018-11-10 22:10:48 3160次浏览
{setimgtitle(

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)项目团队构思,苏州网站建设整理!

@title='ThinkPHP搭建cms-图片上传插件封装')}

技术分享

苏南名片

  • 邮箱:message@jswusn.com
  • 地址:江苏省苏州市相城区

热门文章

Copyright © 2018-2022 jswusn.com 版权所有

技术支持:苏州网站建设  苏ICP备18036849号