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

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

May 20, 2016 am 11:39 AM

作者:白狼 出处: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>
Salin selepas log masuk

 

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

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

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

Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn

Alat AI Hot

Undresser.AI Undress

Undresser.AI Undress

Apl berkuasa AI untuk mencipta foto bogel yang realistik

AI Clothes Remover

AI Clothes Remover

Alat AI dalam talian untuk mengeluarkan pakaian daripada foto.

Undress AI Tool

Undress AI Tool

Gambar buka pakaian secara percuma

Clothoff.io

Clothoff.io

Penyingkiran pakaian AI

AI Hentai Generator

AI Hentai Generator

Menjana ai hentai secara percuma.

Alat panas

Notepad++7.3.1

Notepad++7.3.1

Editor kod yang mudah digunakan dan percuma

SublimeText3 versi Cina

SublimeText3 versi Cina

Versi Cina, sangat mudah digunakan

Hantar Studio 13.0.1

Hantar Studio 13.0.1

Persekitaran pembangunan bersepadu PHP yang berkuasa

Dreamweaver CS6

Dreamweaver CS6

Alat pembangunan web visual

SublimeText3 versi Mac

SublimeText3 versi Mac

Perisian penyuntingan kod peringkat Tuhan (SublimeText3)