Last active
January 14, 2020 04:20
-
-
Save Markvandersteen/75cb0a7695813836f28d to your computer and use it in GitHub Desktop.
Simple Express image route upload for CKEditor with multer middleware
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
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