简体   繁体   中英

CKEditor: Image uploaded but still getting an alert saying can't upload file:filename (using ckfinder) in react

I am making a text editor for my blog (Using React) and using CKEditor for it. I am using an express server on a windows machine to handle the image upload request. When I browse the uploads directory on the windows machine, the file is present but on the CKEditor page I get the following error:

在此处输入图像描述

This is the CKEditor component code (using react):

<CKEditor 
                editor={ClassicEditor}
                data='<p>Hello World</p>'
                onChange={(event,editor) => {
                    setHtml(editor.getData());
                    console.log(html)
                }}
                config={
                    {
                        ckfinder: {
                            uploadUrl:'http://localhost:8000/upload'
                        }
                    }
                }
            />


This is the server code (Using express):

const express = require('express');
const PORT = 8000;
const app = express();
const bodyparser = require('body-parser'); //Body parsing middle ware
const morgan = require('morgan'); //HTTP request logger middle ware
const multipart = require('connect-multiparty');
const MultipartMiddleWare = multipart({uploadDir:'./uploads'});
const cors = require('cors'); // Middle ware to handle cors

app.use(cors())
app.use(bodyparser.urlencoded({extended: true}));
app.use(bodyparser.json());
app.get('/', (req, res) => {
    res.status(200).json({
        message: "Test MSG"
    })
})

app.post('/upload',MultipartMiddleWare,(req,res) => {
    res.send("Success");
    console.log(req.files.upload)
})

app.listen(PORT, console.log(`server has successfully startet at PORT: ${PORT}`))

The correct response for uploaded image is

{
   uploaded: true,
   url: 'path to uploaded image'
}

so in your app.post use the following code:

app.post('/upload',MultipartMiddleWare,(req,res) => {

    let tempFile = req.files.upload;
    let path = tempFile.path;

    res.status(200).json({
        uploaded: true,
        url: `http://localhost:8000/${path}`
    })
})

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