简体   繁体   English

CKEditor文件上传在Yii2中不起作用

[英]CKEditor file upload not working in Yii2

I'm working on Yii2 Project where I've integrated http://ckeditor.com/demo 我正在开发Yii2项目,我已经集成了http://ckeditor.com/demo

Now I want to implement functionality of file/image upload in it. 现在我想在其中实现文件/图像上传的功能。

This is how I integrated CKEditor in Yii2 Project. 这就是我在Yii2 Project中集成CKEditor的方法。

Step1: AppAsset.php 第1步:AppAsset.php

public $js = [
    'ckeditor/ckeditor.js',
];

Calling ckeditor.js javascript file from config/AppAsset Class config/AppAsset类调用ckeditor.js javascript文件

Step2: View 第2步:查看

<?= $form->field($model, 'standard_output_information')->textarea(['rows' => 2, 'class'=>'ckeditor']) ?>

Step3: config.js 第3步:config.js

CKEDITOR.editorConfig = function (config) {   
    var base_url = window.location.origin;
    var pathArray = window.location.pathname.split('/');
    var projectUrl = base_url + "/" + pathArray[1] + "/" + pathArray[2] + "/" + pathArray[3] + "/uploads";

    config.filebrowserImageBrowseUrl = base_url + "/pcwf" + "/backend" + "/web" + "/businessprocessprofile" + "/upload";
    config.filebrowserImageUploadUrl = base_url + "/pcwf" + "/backend" + "/web" + "/businessprocessprofile" + "/upload";    
};

Here I've configured ImageBrowserUrl and ImageUploadUrl mentioned here http://docs.cksource.com/CKEditor_3.x/Developers_Guide/File_Browser_(Uploader) 这里我配置了ImageBrowserUrl和ImageUploadUrl这里提到http://docs.cksource.com/CKEditor_3.x/Developers_Guide/File_Browser_(Uploader)

Step4: Controller 第四步:控制器

public function actionUpload() {
    echo \yii\helpers\Html::csrfMetaTags();

    echo "<pre>";
    print_r($_FILES);
    print_r($_REQUEST);
    exit;
}

Here I'm expecting uploaded image data. 在这里,我期待上传的图像数据。 But whenever I click on Send it to Server button after selection of an image its giving me following error. 但是,每当我在选择图像后单击“ Send it to Server按钮时,它会给我以下错误。

在此输入图像描述

Not sure whether its issue of wrong url configuration in config.js or is it Yii2 form submission issue. 不确定它在config.js中是否存在错误的url配置问题,或者是Yii2表单提交问题。

Any help would be appreciated. 任何帮助,将不胜感激。

I believe you have a problem with the CRSF tokens. 我相信你的CRSF令牌有问题。 Read more about the security here: http://www.yiiframework.com/doc-2.0/guide-security-best-practices.html . 在此处阅读有关安全性的更多信息: http//www.yiiframework.com/doc-2.0/guide-security-best-practices.html The easiest way to get around this is to disable CRSF for that particular action. 解决此问题的最简单方法是为该特定操作禁用CRSF。 You can take a look on how to do that here: https://stackoverflow.com/a/28526946/1880627 您可以在这里查看如何执行此操作: https//stackoverflow.com/a/28526946/1880627

Rather than disabling CSRF validation it is far better and more secure to submit the token with the form to pass server-side validation. 而不是禁用CSRF验证,提交带有表单的令牌以传递服务器端验证会更好,更安全。 This can be quite easily done by injecting a hidden input field into the upload form with javascript: 通过使用javascript将隐藏的输入字段注入上传表单,可以非常轻松地完成此操作:

$(document).off('click', '.cke_dialog_tabs a[id^="cke_Upload_"]').on('click', '.cke_dialog_tabs a[id^="cke_Upload_"]', function () {
    var $forms = $('.cke_dialog_ui_input_file iframe').contents().find('form');
    var csrfName = yii.getCsrfParam();
    $forms.each(function () {
        if (!$(this).find('input[name=' + csrfName + ']').length) {
            var csrfTokenInput = $('<input/>').attr({
                'type': 'hidden',
                'name': csrfName
            }).val(yii.getCsrfToken());
            $(this).append(csrfTokenInput);
        }
    });
});

For a more detailed discussion on the issue refer to 2amigos/yii2-ckeditor-widget, issue #2 . 有关该问题的更详细讨论,请参阅2 amigos/yii2-ckeditor-widget,问题#2 This is also where the code snippet is taken from, with a few minor tweaks to cover the case of multiple widgets on the page. 这也是取自代码片段的地方,只需进行一些小的调整即可覆盖页面上多个小部件的情况。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM