Sid Heart
Sid Heart

Reputation: 743

upload image to ckeditor in laravel Incorrect server response

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>';
}

Upvotes: 10

Views: 11150

Answers (6)

Azizullah Saeidi
Azizullah Saeidi

Reputation: 39

in laravel 7

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

Upvotes: 0

Vishva Madumal
Vishva Madumal

Reputation: 45

set the these propertis in your file

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

Upvotes: 1

Aslam Patel
Aslam Patel

Reputation: 894

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, ]);

Upvotes: 2

DisgruntledGoat
DisgruntledGoat

Reputation: 72510

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

config.filebrowserUploadMethod = 'form';

Upvotes: 17

Suvriti Gandhi
Suvriti Gandhi

Reputation: 185

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.

Upvotes: 3

Tornike Menabde
Tornike Menabde

Reputation: 126

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

return response()->json(['data' => $res]);

Upvotes: 0

Related Questions