10

i am trying to upload image through ckeditor 4 enter image description here

when i press send it the server getting this Error Incorrect Server Response

here is my controller

public function mediauploadpost(Request $request){
    $CKEditor = $request->input('CKEditor');
    $funcNum  = $request->input('CKEditorFuncNum');
    $message  = $url = '';
    if (Input::hasFile('upload')) {
        $file = Input::file('upload');
        if ($file->isValid()) {
            $filename =rand(1000,9999).$file->getClientOriginalName();
            $file->move(public_path().'/wysiwyg/', $filename);
            $url = url('wysiwyg/' . $filename);
        } else {
            $message = 'An error occurred while uploading the file.';
        }
    } else {
        $message = 'No file uploaded.';
    }
    return '<script>window.parent.CKEDITOR.tools.callFunction('.$funcNum.', "'.$url.'", "'.$message.'")</script>';
}
Sid Heart
  • 743
  • 3
  • 14
  • 38
  • After file uploaded return this response to ckeditor: ` return response()->json([ 'fileName' => $filename, 'uploaded' => true, 'url' => $url ]);` – Muhammad Shahzad Aug 07 '21 at 14:03

6 Answers6

17

I had this same issue recently, and the solution was to add this line to my ckeditor-config.js file:

config.filebrowserUploadMethod = 'form';
DisgruntledGoat
  • 70,219
  • 68
  • 205
  • 290
3

If you are using java as your backend server: return string response:

 "{\n " +
 "    \"uploaded\": 1,\n" +
 "    \"fileName\": \"foo.jpg\",\n" +
 "    \"url\": \"/files/foo.jpg\"\n" +
 "}"

See this link fro more information. Also remember to to have annotation: produces = MediaType.TEXT_HTML_VALUE over your rest API.

Suvriti Gandhi
  • 185
  • 1
  • 2
  • 10
  • 1
    I've checked via developer tools the example [here](https://ckeditor.com/docs/ckeditor4/latest/examples/image.html) and they use same response @suvriti-gandhi said. Here is the code I used in Laravel: `return response()->json([ 'fileName' => $filename, 'uploaded' => true, 'url' => $url, ]);` – bondif Jun 17 '19 at 01:38
2

Remove this one

$res = "<script>window.parent.CKEDITOR.tools.callFunction(" .$funcNum.  "," . $url . "," .$message. ")</script>"

Use this code in your return .

return response()->json([ 'fileName' => 'your file name put here', 'uploaded' => false, 'url' => $url, ]);
Aslam Patel
  • 874
  • 6
  • 19
0
$res = "<script>window.parent.CKEDITOR.tools.callFunction(" .$funcNum.  "," . $url . "," .$message. ")</script>"

return response()->json(['data' => $res]);
Tornike Menabde
  • 116
  • 2
  • 5
  • 1
    While this may solve the question, a more comprehensive answer would be to explain what the error was and how this solution fixes that. – Ross Aug 17 '18 at 18:40
  • 1
    While this code snippet may solve the question, [including an explanation](http://meta.stackexchange.com/questions/114762/explaining-entirely-code-based-answers) really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion. – 31piy Aug 18 '18 at 05:20
  • 1
    Thanks for replying me but its not working i found `$CKEditor = $request->input('CKEditor');` and `$funcNum = $request->input('CKEditorFuncNum');` both request coming null how to fix this – Sid Heart Aug 18 '18 at 10:58
0

set the these propertis in your file

<script>
    CKEDITOR.replace('textarea_id',{
      filebrowserUploadUrl:"upload.php",
      filebrowserUploadMethod: "form"
    });
</script>
0

in laravel 7

CKEDITOR.replace('body', {
    filebrowserUploadUrl:'/admin/general-topic/images', // Route
    filebrowserImageUploadUrl:'/admin/general-topic/images', // File location
    filebrowserUploadMethod: "form"
})