【问题标题】:I have a problem with uploading my images with ckeditor我在使用 ckeditor 上传图片时遇到问题
【发布时间】:2019-09-09 16:16:41
【问题描述】:

我尝试使用 ckeditor 上传图像,但我的问题是图像上传到服务器文件夹,但在我的 ckeditor 文本区域中没有显示,它显示服务器响应错误,请问有什么帮助吗? 这是我的代码:

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


        });
             });

这是我的 ckeditor

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

这是我的 ckeditor 配置文件:

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']


    ];
};

【问题讨论】:

    标签: javascript java node.js mongoose ckeditor


    【解决方案1】:

    您不必在较新版本的 ckeditor 中发送整个 HTML。 您应该发送如下所示的 URL:

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

    如果您在本地主机上工作 将 SERVER_URL 替换为 http://localhost:3000

    另一件事,你必须设置

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

    如果您想使用上述 URL 访问图像。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-08-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-07-31
      • 2021-07-08
      • 2018-09-28
      • 1970-01-01
      相关资源
      最近更新 更多