简体   繁体   English

使用CKeditor上传文件

[英]Upload files using CKeditor

I am using ckeditor and I am trying to upload file. 我正在使用ckeditor,并且正在尝试上传文件。 This is my code: 这是我的代码:

    @extends('app')
@section('header')
<script type="text/javascript" src="{{url('ckeditor/ckeditor.js')}}"></script>
@endsection
@section('content')
<div class="container">

    @include('common.errors')
<div class="row">
                <div class="col-lg-12">

                            <div class="row">
                                <div class="col-lg-9">
    {!! Form::model($info, ['route' => ['infos.update', $info->id], 'method' => 'patch','files' => true]) !!}

        @include('infos.fields')

    {!! Form::close() !!}
    </div>
    </div>
    </div>
    </div>
</div>
@endsection
@section('footer')
<script type="text/javascript">
    CKEDITOR.replace('editor1',{

        filebrowserImageUploadUrl : "{{route('infos.upload')}}",
        filebrowserWindowWidth  : 800,
        filebrowserWindowHeight : 500
    });
    window.opener.CKEDITOR.tools.callFunction(CKEditorFuncNum,url);
</script>
@endsection

Whenever I try to upload file, I get token mismatch error. 每当我尝试上传文件时,都会出现令牌不匹配错误。 The form builder automatically adds a hidden csrf field in the main form but since the file upload(POST request) from ckeditor happens through ajax it is giving me that error. 表单生成器会自动在主表单中添加一个隐藏的csrf字段,但是由于ckeditor的文件上载(POST请求)通过ajax发生,因此给了我这个错误。 I know that I can disable this error but when I googled I found out that it was a bad practise to disable it. 我知道可以禁用此错误,但是当我用Google搜索时发现禁用此错误是一种不好的做法。 How can I stop getting this error and upload files? 如何停止收到此错误并上传文件?

Ok so I finally got it working. 好的,所以我终于开始工作了。 I send the csrf token with the request. 我发送带有请求的csrf令牌。

filebrowserImageUploadUrl : "{{route('infos.upload',['_token' => csrf_token() ])}}",

Fixed the Csrf issue with the code below: 使用以下代码修复了Csrf问题:

<script type="text/javascript">
CKEDITOR.replace('editor1',{

    filebrowserImageUploadUrl : "{{ url('your_url?_token='.csrf_token()) }}",

});
</script>

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

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