nodejs 整合kindEditor實現(xiàn)圖片上傳
kindEditor官網(wǎng)上中提供了ASP,ASP.NET,JSP相關(guān)的整合應(yīng)用,http://kindeditor.net/docs/upload.html可以參照實現(xiàn)nodejs的整合,發(fā)現(xiàn)實用nodejs更簡單
環(huán)境:
unbuntu 14.10
nodejs 0.10.35
express 4.11.2
formidable 1.0.16
kindEditor 4.1.10
webStorm 8
1.通過IDE或終端創(chuàng)建一個名稱為test的工程
2.編輯package.json添加formidable依賴,這里使用的是1.0.16版本,之后通過終端執(zhí)行npm install完成依賴的安裝
3.將kindEditor整個目錄放到test/public/lib下
4.修改index.ejs和index.js文件
index.ejs中整合kindEditor:
設(shè)置kindEditor的uploadJson為nodejs所提供的處理圖片上傳的路由url這里用的是/uploadImg
index.js中添加處理圖片上傳的路由url:
添加/uploadImg對應(yīng)的post處理方式,
代碼如下:
index.js
<!DOCTYPE html>
<html>
<head>
<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>
</head>
<body>
<h1><%= title %></h1>
<textarea id="editor" name="content" style="width:700px;height:300px;">
<strong>HTML內(nèi)容</strong>
</textarea>
</body>
</html>
index.js
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: '圖片上傳' });
});
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;
之后通過IDE或終端啟動test工程,通過http://localhost:3000訪問頁面就可以上傳圖片了
相關(guān)文章
nodejs express配置自簽名https服務(wù)器的方法
這篇文章主要介紹了nodejs express配置自簽名https服務(wù)器的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-05-05實例詳解Nodejs 保存 payload 發(fā)送過來的文件
這篇文章主要介紹了實例詳解Nodejs 保存 payload 發(fā)送過來的文件 的相關(guān)資料,需要的朋友可以參考下2016-01-01node.js支持多用戶web終端實現(xiàn)及安全方案
這篇文章主要介紹了node.js支持多用戶web終端實現(xiàn)方案以及web終端安全性保證的解決方法,一起學(xué)習(xí)參考下。2017-11-11nodejs利用ajax實現(xiàn)網(wǎng)頁無刷新上傳圖片實例代碼
本篇文章主要介紹了nodejs利用ajax實現(xiàn)網(wǎng)頁無刷新上傳圖片實例代碼,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-06-06