Skip to content

Instantly share code, notes, and snippets.

@mikekunze
Last active December 19, 2015 15:58
Show Gist options
  • Save mikekunze/5980077 to your computer and use it in GitHub Desktop.
Save mikekunze/5980077 to your computer and use it in GitHub Desktop.
Simple uploader Express route for CKEditor's Image Upload feature
fn = function(req, res) {
var dest, fileName, fs, l, tmpPath;
fs = require('fs');
tmpPath = req.files.upload.path;
l = tmpPath.split('/').length;
fileName = tmpPath.split('/')[l - 1] + "_" + req.files.upload.name;
dest = __dirname + "/public/uploads/" + fileName;
fs.readFile(req.files.upload.path, function(err, data) {
if (err) {
console.log(err);
return;
}
fs.writeFile(dest, data, function(err) {
var html;
if (err) {
console.log(err);
return;
}
html = "";
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);
});
});
};
export.modules = fn
@RandyLedbetter
Copy link

Thank you very much for sharing this. I've been struggling with getting the image2 widget to call window.parent.CKEDITOR.tools.callFunction from my Express route. Sending the html string back was key.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment