Nodejs integration Kindeditor realize picture upload _node.js

Source: Internet
Author: User

Kindeditor Official online provides asp,asp.net,jsp related integration applications, http://kindeditor.net/docs/upload.html can be referenced to achieve NODEJS integration, found that practical nodejs simpler

Environment:
Unbuntu 14.10
Nodejs 0.10.35
Express 4.11.2
Formidable 1.0.16
Kindeditor 4.1.10
Webstorm 8

1. Create a project called Test through the IDE or the terminal

2. Edit Package.json Add formidable dependencies, here is a 1.0.16 version, after which the NPM install completes the dependent installation via the terminal

3. Place Kindeditor entire catalogue under Test/public/lib

4. Modification of Index.ejs and index.js documents
Integrated Kindeditor in Index.ejs:
Set Kindeditor Uploadjson for Nodejs the route URL that is provided for processing picture upload here's the/uploadimg.
Index.js to add a route URL to handle picture uploads:
Add/uploadimg the corresponding post processing method,
The code is as follows:

Index.js

Copy Code code as follows:

<! DOCTYPE html>
<title><%= title%></title>
<link rel= ' stylesheet ' href= '/stylesheets/style.css '/>
<script charset= "Utf-8" src= "/lib/kindeditor-4.1.10/kindeditor.js" ></script>
<script charset= "Utf-8" src= "/lib/kindeditor-4.1.10/lang/zh_cn.js" ></script>
<script>
var options = {
Uploadjson: '/uploadimg '
};
Kindeditor.ready (function (K) {
Window.editor = k.create (' #editor ', options);
});
</script>
<body>
<textarea id= "editor" name= "Content" style= "width:700px;height:300px;" >
<strong>html content </strong>
</textarea>
</body>

Index.js

Copy Code code as follows:

var express = require (' Express ');
var router = Express. Router ();
var formidable = require (' formidable ');
/* Get home page. */
Router.get ('/', function (req, res, next) {
Res.render (' index ', {title: ' Picture Upload '});
});
Router.post ('/uploadimg ', function (req, res, next) {
var form = new Formidable. Incomingform ();
Form.keepextensions = true;
Form.uploaddir = __dirname + '/... /public/upload ';
Form.parse (req, Function (Err, fields, files) {
if (err) {
throw err;
}
var image = Files.imgfile;
var path = Image.path;
Path = Path.replace ('/\\/g ', '/');
var url = '/upload ' + path.substr (path.lastindexof ('/'), path.length);
var info = {
"Error": 0,
"url": URL
};
Res.send (info);
});
});
Module.exports = router;

Then start the test project through the IDE or the terminal, and you can upload the picture through the http://localhost:3000 access page.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.