[英]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.我正在为我的博客(使用 React)制作一个文本编辑器,并为此使用 CKEditor。 I am using an express server on a windows machine to handle the image upload request.
我在 windows 机器上使用快速服务器来处理图像上传请求。 When I browse the uploads directory on the windows machine, the file is present but on the CKEditor page I get the following error:
当我浏览 windows 机器上的上传目录时,该文件存在,但在 CKEditor 页面上出现以下错误:
This is the CKEditor component code (using react):这是 CKEditor 组件代码(使用反应):
<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 中使用以下代码:
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}`
})
})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.