简体   繁体   中英

I have a problem with uploading my images with ckeditor

I have tried to upload images with ckeditor but my problem is that the images upload to the server folders but ddoesnt show in my ckeditor text area , it show server response error , any help please ? This is my code :

 router.post('/upload&responseType=json', function(req, res) {
        var fs = require('fs');

        var tmpPath = req.files.upload.name;
        l = tmpPath.split('/').length;`enter code here`
        var fileName = tmpPath.split('/')[l - 1] + "_" + "s";
            var buf = new Buffer.from(req.files["upload"].data);
            var newPath ='public/uploads/'+tmpPath; 
            console.log(newPath);
            console.log(tmpPath);
            console.log(fileName);
                fs.writeFile(newPath,buf, function (err) {
                if (err) console.log({err: err});
                else {

             html = "uploaded";
             html += "<script type='text/javascript'>";
             html += "    var funcNum = " + req.query.CKEditorFuncNum + ";";
             html += "    var url     = \"/uploads/" + fileName;
             html += "    var message = \"Uploaded file successfully\";";
             html += "";
             html += "    window.parent.CKEDITOR.tools.callFunction(funcNum, url, message);";
             html += "</script>";

            res.send(html);
                }


        });
             });

This is my ckeditor

CKEDITOR.config.customConfig = '/js/ckeditor_config.js';
CKEDITOR.replace(editor2,{ filebrowserUploadUrl: '/upload', });

And this my ckeditor config file :

CKEDITOR.editorConfig = function( config )
{


config.filebrowserUploadMethod = 'form';  
    config.toolbar = 'MyToolbar';

    config.toolbar_MyToolbar =
    [
        ['Source','Templates'],
        ['Cut','Copy','Paste','SpellChecker','-','Scayt'],
        ['Undo','Redo','-','Find','Replace'],
        ['Image','Flash','Table','HorizontalRule','Smiley','SpecialChar','PageBreak'],
      ['Maximize','-','About'],
        '/',
       ['Bold','Italic','Underline','Strike','-','Subscript','Superscript'],      
        ['NumberedList','BulletedList','-','Outdent','Indent','Blockquote','SelectAll','RemoveFormat'],
        ['Link','Unlink','Anchor'],
      ['Styles','Format','Font','FontSize'],
       ['TextColor','BGColor']


    ];
};

You do not have to send the whole HTML in the newer versions of ckeditor.. You are just supposed to send the URL as shown below:

res.send({
    url: "<SERVER_URL>/public/uploads/" + fileName,
})

If you are working on localhost replace SERVER_URL with something like http://localhost:3000

Another thing, you have to set

app.use(express.static('public/uploads'));

If you want to access the image using the URL mentioned above.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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