Yii2 solves the problem of Baidu editor umeditor Image uploading-Wolf stack

Source: Internet
Author: User
Yii2 solves the problem of Baidu editor umeditor Image uploading-author of the White Wolf stack: White Wolf Source: Ghost.

The yii2 Framework integrates the Baidu editor. because the file is uploaded using the UploadedFile provided by yii2, the umeditor Upload will inevitably fail. to solve the problem, only two steps are required, let's take a look at the specific implementation

First, we need to configure umeditor. here we only need to change the imageUrl configuration item. we can modify it to point to/tools/um-upload.

The next step is to implement the/tools/um-upload method,

According to the ueditor implementation, we only need to return the successful message after the upload is successful.

Use backend \ models \ Upload; use yii \ web \ UploadedFile;/*** Baidu umeditor Upload */public function actionUmUpload () {$ model = new Upload (); if (Yii ::$ app-> request-> isPost) {$ model-> file = UploadedFile: getInstance ($ model, 'file '); $ dir = 'File save directory'; if (! Is_dir ($ dir) mkdir ($ dir); if ($ model-> validate () {$ fileName = $ model-> file-> baseName. ". ". $ model-> file-> extension; $ dir = $ dir. "/". $ fileName; $ model-> file-> saveAs ($ dir); $ info = ["originalName" => $ model-> file-> baseName, "name" => $ model-> file-> baseName, "url" => $ dir, "size" => $ model-> file-> size, "type" =>$ model-> file-> type, "state" => "SUCCESS",]; exit (json_encode ($ info ));}}}

Note: in the $ info returned above, the state can only be SUCCESS, case sensitive.

For more information about Yii image upload, see Yii2 file upload.

For more information about yii integration Baidu editor, see yii2 integration Baidu editor umeditor

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.