作者:白狼 出处: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

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

Notepad++7.3.1
Easy-to-use and free code editor

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function
