thinkphp wangEditor image upload

1.html

 

< Div the above mentioned id = "Editor" > 
</ div > 
<-!
WangEditor name attribute can not be set
to upload to add a hidden the TextArea
->
<the TextArea the above mentioned id = "TAREA" name = "Content" hidden> </ the TextArea>



< ! - the introduction of the JS wangEditor -> < Script type = "text / JavaScript" the src = "the STATIC __ __ / wangEditor / Release / wangEditor.min.js" > </ Script > <-! configuration wangEditor -> < Script > var E = window. wang editor var editor = new A ( ' #editor ' )// upload pictures to the server editor.customConfig.uploadImgServer = ' / the Upload ' ; // The image size is limited to 3M editor.customConfig.uploadImgMaxSize = 3 * 1024 * 1024 ; // limiting upload up to three pictures editor.customConfig. uploadImgMaxLength = . 3 ; // wangEditor monitor changes to update the contents of the textarea var $ TAREA = $ ( '# TAREA');
editor.customConfig.onchange = function (HTML) {
$ tarea.val (HTML)
};
Editor. Create ();
$ tarea.val (editor.txt.html ());
</ Script >

 

 

 

 2.tp5

    To set routing: upload

 

Route::rule('upload','index/index/upload');

 

 

    Method index added upload controller:

public function upload()
    {
        $files = request()->file();
        $imags = [];
        $errors = [];
        foreach($files as $file){
            // 移动图片到/public/uploads/ 目录下
            $info = $file->move(ROOT_PATH . 'public' . DS . 'uploads');
            if($info){
               // 成功上传 图片地址加入到数组
                $path = '/uploads/'.$info->getSaveName();
                array_push($imags,$path);
            }else{
                array_push($errors,$file->getError());
            }
        }

        //输出wangEditor规定的返回数据
        if(!$errors){
            $msg['errno'] = 0;
            $msg['data'] = $imags;
            return json($msg);
        }else{
            $msg['errno'] = 1;
            $msg['data'] = $imags;
            $msg['msg'] = "上传出错";
            return json($msg);
        }
    }    

 

3.提交表单

 

Guess you like

Origin www.cnblogs.com/xsly/p/11105954.html