一聚教程网:一个值得你收藏的教程网站

热门教程

Dedecms5.7实现批量上传图片二次开发

时间:2022-06-25 19:26:53 编辑:袖梨 来源:一聚教程网

实现步骤:
1.下载5.6 ,去官方下载5.6的版本,注意编码要一致!
include文件夹下把FCKeditor文件夹复制到5.7的include的文件夹下!
2.修改后台配置:如图所示,改成fck

Dedecms5.7实现批量上传图片二次开发

ckeditor图片批量上传(Zend+PHP)
1:要求
项目中的一个要求,原来后台的商品图片只能一张一张的上传,现在要求优化为一次可以上传多张,并且直接插入到编辑器中
2:思路
仍采用原来上传图片的程序,只是在上传页面中添加多个上传输入框,提交时候,将所有文件都提交到后台控制器,修改控制器处理代码以及回调函数
3:步骤
(1)  新增图片批量上传tab标签
       1.1    fckeditor/editor/lang/zh-cn.js  文件新增tab变量声明
                DlgMultiUpload      : "批量上传图片",

        1.2   fckeditor/editor/dialog/fck_image/fck_image.js   文件新增tab标签调用

 代码如下 复制代码


        if ( FCKConfig.ImageUpload )
       dialog.AddTab( 'MultiUpload', FCKLang.DlgMultiUpload ) ;

      if(tabCode =="MultiUpload")//此tab标签页面还有一个相册分类需要获取
    {
        dialog.Sizer.ResizeDialog(450,450);//初始化弹出框的大小
        $.getJSON('/picture/cate/get',{},function(data){//在其他项目中这个不是必须的
            if(data.items.length>0)
            {
                $("#multi_albumlist").empty();
                $.each(data.items,function(i,cate){
                    var option = new Option(cate.cate_name,cate.cate_id);
                    $("#multi_albumlist").append(option);
                });               
            }
            else
            {
                var option = new Option('','');
                $("#multi_albumlist").append(option);
            }
            $("#multi_albumlist").show();
        });           
    }   


      1.3      fckeditor/fckconfig.js  文件新增图片批量上传Url
                 FCKConfig.ImageMultiUploadURL ='/picture/upload/multisave';
 (2)  页面布局设置
        fckeditor/editor/dialog/fck_image.html
             


(3) 提交时js验证
function CheckMultiUpload()
{
    var sFile1 = GetE('txtUploadFile1').value ;
    var sFile2 = GetE('txtUploadFile2').value ;
    var sFile3 = GetE('txtUploadFile3').value ;
    var sFile4 = GetE('txtUploadFile4').value ;
    var sFile5 = GetE('txtUploadFile5').value ;
    var sFile6 = GetE('txtUploadFile6').value ;
    if ( sFile1.length == 0 && sFile2.length == 0 && sFile3.length == 0 && sFile6.length == 0 && sFile5.length == 0 && sFile6.length == 0)
    {
        alert( '请至少上传一张图片' ) ;
        return false ;
    }
    if ( ( FCKConfig.ImageUploadAllowedExtensions.length > 0 && !oUploadAllowedExtRegex.test( sFile ) ) ||
        ( FCKConfig.ImageUploadDeniedExtensions.length > 0 && oUploadDeniedExtRegex.test( sFile ) ) )
    {
        OnUploadCompleted( 202 ) ;
        return false ;
    }

    // Show animation
    window.parent.Throbber.Show() ;
    GetE( 'divMultiUpload' ).style.display  = 'none' ;

    return true ;
}
(4)回调函数
function callback_multisubmit(status, errid,html){
    if(status==0)
    {
        //将图片直接插入到编辑器中
        var oParentEditor=window.parent.InnerDialogLoaded().FCK;
        oParentEditor.InsertHtml(html);
        dialog.CloseDialog();
        return;
    }
    switch(errid)
    {
        case 0 :
            alert('该图片未上传成功,请重新上传。');
            break;
        case 1 :
            alert('该照片未上传成功,相册空间不足。');
            break;
        case 2 :
            alert('您选择的这张照片超过2MB,请重新选择。');
            break;
        case 3:
            alert('图片格式不正确,您可以上传JPG、GIF和PNG格式的照片。');
            break;
        default : alert('该图片未上传成功,请重新上传。');
    }
}
(5)提交后控制器处理

        $img_url = array();
        $img_title = array();
        for($i=0;$i<$count;$i++)
        {          
                    $file_name = "MultiPhotoFile".$i."_f";
                    if(!empty($_FILES [$file_name]["tmp_name"]))
                    {
                        $file = new Henu_UploadFile ( '/temp/', $_FILES [$file_name] ["tmp_name"], $_FILES [$file_name] ["name"], $_FILES [$file_name] ["size"], $_FILES [$file_name] ["type"] );
                        $check_res = $file->check ();
                        if ($check_res !== true)
                        {
                            //报错
                            if ($check_res == 1)
                            {
                                echo "";
                            }
                            elseif ($check_res == 2)
                            {
                                echo "";
                            }
                            return;
                        }
                        $img_info = $file->upSend (); //返回上传文件信息   
                        if ($img_info === false)
                        {
                            //报错
                            echo "";
                            return;
                        }
                        $title = $img_info ["OldName"];
                        $filetype = $img_info ["filetype"];
                        switch ($filetype)
                        {
                            case "image/jpg" :
                                $filetype = "jpg";
                                break;
                            case "image/gif" :
                                $filetype = "gif";
                                break;
                            case "image/png" :
                                $filetype = "png";
                                break;
                            default :
                                $filetype = "jpg";
                        }
                        //如果是商品上传,都处理成jpg
                        if ($pic_f == "goods_add")
                        {
                            $filetype = "jpg";
                        }
                        /**
                         * 调用图片上传
                         */
                        $client->setConfig ( "upload", "index", "POST", array ("store_id" => $store_id, "cate_id" => $cate_id, "file_type" => $filetype, "title" => htmlspecialchars ( $title ), "water" => $water, "compress" => $compress, "ip" => ip2long ( real_ip () ) ), "picture" );
                        $http = $client->getHttp ();
                        $http->setFileUpload ( $img_info ["pictureTruePath"], 'img' );
                        $res = $client->getData ();
                        if (isset ( $res ["rsp"] ["img"] ))
                        {
                            /**
                             * 图片上传成功
                             */
                            @unlink ( $img_info ["pictureTruePath"] );
                            if ($pic_f == "goods_add") //商品上传
                            {
                                https://img.111cn.net$img_url[$i] = $res ["rsp"] ["img"] ["picture_path"] . '.' . $res ["rsp"] ["img"] ["file_type"];
                                $img_title[$i] = $title;
                           
                            }
                            else
                            {
                                echo "";
                            }
                        }
                        else
                        {
                            echo "";
                        }
                    }
        }
        for($i=0;$i<=$count;$i++)
        {  
            if(!empty(https://img.111cn.net$img_url[$i]))
            {
                $callback_temp = "$img_title[$i]
";
                $callback_html .= $callback_temp;
            }
        }
        echo '';
    }

热门栏目