Ionaru / easy-markdown-editor

EasyMDE: A simple, beautiful, and embeddable JavaScript Markdown editor. Delightful editing for beginners and experts alike. Features built-in autosaving and spell checking.
https://stackblitz.com/edit/easymde
MIT License
2.39k stars 316 forks source link

EasyMDE: The server did not return a valid json. #227

Open KoljaL opened 4 years ago

KoljaL commented 4 years ago

I got this message after uploading an image:

EasyMDE: The server did not return a valid json.

My code is the following:

new EasyMDE({
    element: document.getElementById('EasyMDE_field'),
    hideIcons: ['image'],
    showIcons: ['upload-image'],
    imageUploadEndpoint: "https://domain.de/CMS/functions/img_upload.php"
});

and in the img_upload.php:

if(isset($_FILES['image'])) {
    $uploaddir = '../data/';
    $uploadfile = $uploaddir . basename($_FILES['image']['name']);

    if(!is_uploaded_file($_FILES['image']['tmp_name'])) {
        echo json_encode(array('error'=>'importError'));
        echo "can't upload";
    }
    if (move_uploaded_file($_FILES['image']['tmp_name'], $uploadfile)) {
        echo json_encode(array('data'=> array('filePath'=>$upload_json))); 
    }
}

The images are getting uploaded, but there will be no link insert in the Textfield, instead i got an error message and the json code loos so:

{"data":{"filePath":"..\/data\/image.png"}}
Ionaru commented 4 years ago

Using your code I was able to upload an image. Double-check your code reaches the echo json_encode(...); and does not fail on move_uploaded_file

KoljaL commented 4 years ago

Hi Ionaru, the upload works, but the link to the image will not be insert. And yes, the echo json_encode will be reached, because i tried some different ways to make filepath:

{"data":{"filePath":"\\data\\Book_Logo.png"}}
{"data":{"filePath":"data\\admin.png"}}

As i wrote, the images are getting uploaded (, if the filepath is written correctly).

Do you have any other wrking example in PHP? On github i only found a one in python.

Thanks for your help.