Skip to content

Instantly share code, notes, and snippets.

@Markvandersteen
Last active January 14, 2020 04:20
Show Gist options
  • Save Markvandersteen/75cb0a7695813836f28d to your computer and use it in GitHub Desktop.
Save Markvandersteen/75cb0a7695813836f28d to your computer and use it in GitHub Desktop.
Simple Express image route upload for CKEditor with multer middleware
multer = require('multer');
var storage = multer.diskStorage({
destination: function (req, file, cb) {
cb(null, 'public/uploads/images/')
}
});
var upload = multer({ storage: storage });
module.exports = function (app) {
router.post('/', upload.single('upload') ,function(req, res){
var html;
var fs = require('fs');
fs.readFile(req.file.path, function (err, data) {
if(err) {
res.send({error : err})
}
else{
html = "";
html += "<script type='text/javascript'>";
html += " var funcNum = " + req.query.CKEditorFuncNum + ";";
html += " var url = \"/uploads/image/" + req.file.filename + "\";";
html += " var message = \"Uploaded file successfully\";";
html += "";
html += " window.parent.CKEDITOR.tools.callFunction(funcNum, url, message);";
html += "</script>";
res.send(html);
}
});
});
app.use('/image' , router);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment