Last active
February 16, 2024 02:08
-
-
Save RedactedProfile/ac48c270d2bbe739f9f3 to your computer and use it in GitHub Desktop.
CKEditor File Upload with Node and Express
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var express = require('express'); | |
var router = express.Router(); | |
var multipart = require('connect-multiparty'); | |
var multipartMiddleware = multipart(); | |
router.get('/', function(req, res) { | |
res.render('index'); | |
}); | |
router.post('/uploader', multipartMiddleware, function(req, res) { | |
var fs = require('fs'); | |
fs.readFile(req.files.upload.path, function (err, data) { | |
var newPath = __dirname + '/../public/uploads/' + req.files.upload.name; | |
fs.writeFile(newPath, data, function (err) { | |
if (err) console.log({err: err}); | |
else { | |
html = ""; | |
html += "<script type='text/javascript'>"; | |
html += " var funcNum = " + req.query.CKEditorFuncNum + ";"; | |
html += " var url = \"/uploads/" + req.files.upload.name + "\";"; | |
html += " var message = \"Uploaded file successfully\";"; | |
html += ""; | |
html += " window.parent.CKEDITOR.tools.callFunction(funcNum, url, message);"; | |
html += "</script>"; | |
res.send(html); | |
} | |
}); | |
}); | |
}); | |
module.exports = router; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
doctype html | |
body | |
form | |
textarea#ckeditor(name="content") | |
script. | |
CKEDITOR.replace('ckeditor', { | |
filebrowserUploadUrl: '/uploader' | |
}); | |
I always get an error not found (404) with folder uploader in two cases: set static path and not athough i create folder in root or in public folder. Why?
I always get an error not found (404) with folder uploader in two cases: set static path and not athough i create folder in root or in public folder. Why?
Use a dot(.) after +' sign. Like this: __dirname + './../public/uploads/' + req.files.upload.name;
It will save uploaded image to public/uploads So make a directory name uploads inside public directory first.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I have looked for a fully working demo, to guide me step by step but I have failed. I even checked the ckeditor documentation for image upload but I am not understanding anything. Can someone help me with a detailed step to step approach to this problem with clear explanation, or is there a link that I can follow. Thanks