2amigos / yii2-tinymce-widget

TinyMCE WYSIWYG widget for Yii2
http://yiiwheels.com
Other
101 stars 48 forks source link

how integer with image upload? #1

Closed jason-son closed 10 years ago

jason-son commented 10 years ago

I follow the documents and here http://pixabay.com/zh/blog/posts/direct-image-uploads-in-tinymce-4-42/

    <?= $form->field($model, 'content')->widget(TinyMce::className(), [
        'options' => ['rows' => 6],
        'language' => 'zh_CN',
        'clientOptions' => [
            'plugins' => [
                "advlist autolink lists link charmap print preview anchor",
                "searchreplace visualblocks code fullscreen",
                "insertdatetime media table contextmenu paste image"
            ],
            'toolbar' => "undo redo | styleselect | bold italic | alignleft aligncenter alignright alignjustify |  bullist numlist outdent indent | link image",
        'file_browser_callback'=>"function(field_name, url, type, win) {
            if(type=='image') $('#my_form input').click();
        }",
        ]
    ]);?>
    <iframe id="form_target" name="form_target" style="display:none">
    <form id="my_form" action="/upload/" target="form_target" method="post" enctype="multipart/form-data"          style="width:0px;height:0;overflow:hidden">
        <input name="image" type="file" onchange="$('#my_form').submit();this.value='';">
    </form>
    </iframe>

but not work,errors occurs when I click the upload button:

Uncaught TypeError: string is not a function
tonydspaniard commented 10 years ago

You need to use a JsExpression

'file_browser_callback'=> new yii\web\JsExpression("function(field_name, url, type, win) {
            if(type=='image') $('#my_form input').click();
        }"),
SpbSprut commented 9 years ago

tonydspaniard THANKS!!!

qwqcode commented 8 years ago

Thank you so much!!!

daro16 commented 7 years ago

Any chance you could post your view, model and controller parts? Seems that there is a lack of resource when integrating uploads to WYSIWYG editors in Yii2. Thanks in anticipation.

tonydspaniard commented 7 years ago

@SpbSprut @Zneiat if you guys do as requested by @daro16 I would highly recommend to add the sample to the README file. Would be highly appreciated! I am lacking of time to do all the support it requires ;)

daro16 commented 7 years ago

Hi Guys @SpbSprut @Zneiat, I'm attempting to implement but browse button does not trigger? any hints would be greatly appreciated.

diego-betto commented 7 years ago

@daro16 check if the form used for file upload is outside ActiveForm's form tags. Otherwise It will not work (form inside a form :)

diego-betto commented 7 years ago

Also consider csrf token. My final code is

field inside ActiveForm

    <?= $form->field($model, 'text')->widget(TinyMce::className(), [
        'options' => ['rows' => 10],
        'clientOptions' => [
            'plugins' => [
                "advlist autolink lists link charmap print preview anchor",
                "searchreplace visualblocks code fullscreen",
                "insertdatetime media table contextmenu paste image"
            ],
            'toolbar' => "undo redo | styleselect | bold italic | alignleft aligncenter alignright alignjustify | bullist numlist outdent indent | link image",
            'file_browser_callback'=> new yii\web\JsExpression("function(field_name, url, type, win) {
                if(type=='image') {
                        $('#upload_image input[type=\"file\"]').click();
                }
            }"),
        ]
    ]) ?>

and form (outside ActiveForm!)

    <form id="upload_image" action="<?=Url::to('/test/image-upload')?>" target="test-text_ifr" method="post" enctype="multipart/form-data" style="width:0px;height:0;overflow:hidden">
        <input name="image" type="file" onchange="$('#upload_image').submit();this.value='';">
        <input type="hidden" name="_csrf" value="<?=Yii::$app->request->getCsrfToken()?>" />
    </form>
tonydspaniard commented 7 years ago

Thank you @diego-betto very kind.