1

我在 laravel 中使用 CKEditor 实现图像上传,但图像已上传但未显示。我需要帮助,请。

这些是我的代码

 public function upload(Request $request){
      
        if($request->hasFile('upload')){
            $originalName=$request->file('upload')->getClientOriginalName();
            $fileName=pathinfo($originalName,PATHINFO_FILENAME);
            $extension=$request->file('upload')->getClientOriginalExtension();
            $fileName=$fileName.'_'.time().'.'.$extension;

            $request->file('upload')->move(public_path('images'),$fileName);

            $CKEditorFuncNum=$request->input('CKEditorFuncNum');
            $url=asset('public/images/'.$fileName);
            $msg='Image uploaded successfully';
            $response="<script >window.parent.CKEDITOR.tools.callFunction($CKEditorFuncNum,'$url','$msg')</script>";
            @header('content-type:text/html','charset-utf-8');
            echo $response;
        }
    }

输出是这个

在此处输入图像描述

4

1 回答 1

1

把这些脚本放到你的 html 文件中

<script src="https://cdn.ckeditor.com/4.14.1/standard/ckeditor.js"></script>
<script>
   CKEDITOR.replace( 'summary-ckeditor', {
filebrowserUploadUrl: "{{route('ckeditor.upload', ['_token' => csrf_token() ])}}",
filebrowserUploadMethod: 'form'

});

并将这些代码放入您的图像控制器中

     if($request->hasFile('upload')) {
     $originName = $request->file('upload')->getClientOriginalName();
     $fileName = pathinfo($originName, PATHINFO_FILENAME);
     $extension = $request->file('upload')->getClientOriginalExtension();
     $fileName = $fileName.'_'.time().'.'.$extension;
    
     $request->file('upload')->move(public_path('images'), $fileName);

     $CKEditorFuncNum = $request->input('CKEditorFuncNum');
     $url = asset('images/'.$fileName); 
     $msg = 'Image uploaded successfully'; 
     $response = "<script>window.parent.CKEDITOR.tools.callFunction($CKEditorFuncNum, '$url', '$msg')</script>";
           
     @header('Content-type: text/html; charset=utf-8'); 
     echo $response;
 }
于 2020-09-22T15:30:57.283 回答