Maison >php教程 >php手册 >yii2解决百度编辑器umeditor图片上传问题 - 白狼栈

yii2解决百度编辑器umeditor图片上传问题 - 白狼栈

WBOY
WBOYoriginal
2016-05-20 11:39:241074parcourir

作者:白狼 出处:http://www.manks.top/article/yii2_umeditor_upload本文版权归作者,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。

yii2框架整合了百度编辑器,因为文件上传采用的是yii2自带的UploadedFile,这就难免umeditor上传不成功问题,解决问题的只需要两个操作步骤,我们来看看具体实现

首先我们先把umeditor的配置搞好,这里只需要更改imageUrl配置项即可,我们修改其指向/tools/um-upload

那下一步自然是实现/tools/um-upload方法了,

按照ueditor的实现来看,这里我们上传成功后只需要返回成功信息即可

<span style="color: #0000ff;">use</span><span style="color: #000000;"> backend\models\Upload;
</span><span style="color: #0000ff;">use</span><span style="color: #000000;"> yii\web\UploadedFile;
   </span><span style="color: #008000;">/*</span><span style="color: #008000;">*
     *    百度umeditor上传
     </span><span style="color: #008000;">*/</span>
    <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span><span style="color: #000000;"> actionUmUpload ()
    {
        </span><span style="color: #800080;">$model</span> = <span style="color: #0000ff;">new</span><span style="color: #000000;"> Upload();

        </span><span style="color: #0000ff;">if</span> (Yii::<span style="color: #800080;">$app</span>->request-><span style="color: #000000;">isPost) {

            </span><span style="color: #800080;">$model</span>-><span style="color: #008080;">file</span> = UploadedFile::getInstance(<span style="color: #800080;">$model</span>, &<span style="color: #008000;">#</span><span style="color: #008000;">39;file');</span>

            <span style="color: #800080;">$dir</span> =<span style="color: #000000;"> ‘文件保存目录’;
            </span><span style="color: #0000ff;">if</span> (!<span style="color: #008080;">is_dir</span>(<span style="color: #800080;">$dir</span><span style="color: #000000;">))
                </span><span style="color: #008080;">mkdir</span>(<span style="color: #800080;">$dir</span><span style="color: #000000;">);

            </span><span style="color: #0000ff;">if</span> (<span style="color: #800080;">$model</span>-><span style="color: #000000;">validate()) {
                </span><span style="color: #800080;">$fileName</span> = <span style="color: #800080;">$model</span>-><span style="color: #008080;">file</span>-><span style="color: #008080;">baseName</span> . "<span style="color: #008000;">." . $model->file->extension;</span>
                <span style="color: #800080;">$dir</span> = <span style="color: #800080;">$dir</span>."<span style="color: #008000;">/". $fileName;</span>
                <span style="color: #800080;">$model</span>-><span style="color: #008080;">file</span>->saveAs(<span style="color: #800080;">$dir</span><span style="color: #000000;">);

                </span><span style="color: #800080;">$info</span> =<span style="color: #000000;"> [
                    </span>"originalName" => <span style="color: #800080;">$model</span>-><span style="color: #008080;">file</span>-><span style="color: #008080;">baseName</span>,
                    "name" => <span style="color: #800080;">$model</span>-><span style="color: #008080;">file</span>-><span style="color: #008080;">baseName</span>,
                    "url" => <span style="color: #800080;">$dir</span>,
                    "size" => <span style="color: #800080;">$model</span>-><span style="color: #008080;">file</span>->size,
                    "type" => <span style="color: #800080;">$model</span>-><span style="color: #008080;">file</span>->type,
                    "state" => "<span style="color: #008000;">SUCCESS",</span>
<span style="color: #000000;">                ];
                </span><span style="color: #0000ff;">exit</span>(json_encode(<span style="color: #800080;">$info</span><span style="color: #000000;">));
            }  
        }
    }</span>

 

特别提醒:上述返回的$info信息中state状态只能是SUCCESS,区分大小写

关于Yii图片上传请参考Yii2文件上传

关于yii整合百度编辑器请参考yii2整合百度编辑器umeditor

Déclaration:
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn