富文本编辑的组件有很多,大名鼎鼎的KENDO UI中自然也有,但是默认功能中,只能包含网络图片,

而如果要实现本地上传图片,KENDO UI也提供了相应的功能,但必须实现KENDO规定的多个接口,

而且功能过于丰富,有时项目并用不到,所以我想利用自定义按钮来实现,下面就是实现过程:

1、先在JSP中定义textarea标签,作为富文本编辑框的占位。

 <div class="form-group">
<label class="col-xs-2 control-label">项目简述:</label>
<div class="col-xs-8">
<textarea id="project-desc" type="text" class="form-control" maxlength="10000"></textarea>
</div>
</div>

2、在JS脚本中定义其为KendoEditor,并设置其默认按钮,及自定义按钮。

     $("#project-desc").kendoEditor({
tools: [
"formatting",
"bold", "italic", "underline",
"justifyLeft", "justifyCenter", "justifyRight",
"insertUnorderedList", "insertOrderedList", "indent",
"createTable",
{
name: "image",
tooltip: "Insert image",
template: "<button type='button' class='k-button' onclick='uploadimg();'><span class='glyphicon glyphicon-picture' aria-hidden='true'></button>",
}
], keydown: function(e) {
$(".k-editable-area").tooltip('destroy');
}
});

name为标签的名字,tooltip为悬停的提示,template为按钮的样式。

3、uploadimg()方法是打开文件上传选择窗口,这里我使用的是kendoWindow。

JSP代码:

  <div id="upload-img-win">
<div class="container-fluid">
<form id="editorUploadImg" action="${ctx }/Detail/uploadImg" enctype='multipart/form-data'>
<input id="srcEditor" type="hidden"/>
<div class="form-group ld-bottom" id="ImgUploadGroup">
<label class="col-xs-2 control-label">图片上传:</label>
<div class="col-xs-8">
<button id="uploadImg-btn" type="button" class="btn btn-primary" onclick="openImgSelectFile();">选择文件</button>
<label id="uploadImgFileName" class="control-label"></label>
<input id="uploadImg" name="uploadImg" type="file" class="hidden" onchange="seletedImgFile();"/>
</div>
</div>
<div class="row ld-top ld-bottom">
<div class="col-xs-10">
<div class="pull-right">
<button id="doc-save-btn" type="button" class="btn btn-primary" onclick="uploadImgWinObj.save()">保存</button>
<button id="doc-cancel-btn" type="button" class="btn btn-default" onclick="uploadImgWinObj.close()">关闭</button>
</div>
</div>
</div>
</form>
</div>
</div>

js代码:

 var uploadImgWinObj = null;
//上传图片窗口
function uploadImgWin() {
var me = this; this.winEl = $("#upload-img-win");
this.winEl.kendoWindow({
draggable : true,
width : "650px",
modal : true,
pinned : false,
title : "选择图片",
visible : false,
animation : false,
resizable : false,
actions : ["Close"]
}); this.kObj = this.winEl.data("kendoWindow") this.open = function(srcEditor) {
clearInput("#upload-img-win");
$("#uploadImgFileName").html("");
$("#uploadImg").val("");
$("#srcEditor").val(srcEditor);
this.kObj.center();
this.kObj.open();
} this.close = function() {
this.kObj.close();
} this.save = uploadImg;
} //上传图片
function uploadImg(){
if($("#uploadImg").val()==""){
markError("#uploadImg","没有选择任何文件!","#editorUploadImg")
return;
} $("#editorUploadImg").ajaxSubmit({
type: "post",
success: function (data) {
if(data!="-99"){
// bootbox.alert("操作成功!");
var srcEditor = $("#srcEditor").val();
var editor = $(srcEditor).data("kendoEditor");
editor.exec("insertHTML", { value: "<img src='"+ ctx + "/" + data +"' >"});
uploadImgWinObj.close();
}else{
uploadImgWinObj.close();
bootbox.alert("操作失败!");
}
},
error: function(e){
bootbox.alert("操作失败!");
uploadImgWinObj.close();
}
});
} //选择图片
function openImgSelectFile(){
$("#uploadImg").click();
} //选中图片后,显示图片名称
function seletedImgFile(){
$("#uploadImgFileName").html($("#uploadImg").val());
} function uploadimg(){
uploadImgWinObj.open("#project-desc");
} $(document).ready(function() {
uploadImgWinObj = new uploadImgWin();
}

openImgSelectFile和seletedImgFile是对文件选择控件的包装,为了显示效果好看些。

uploadImg方法采用了ajaxSubmit方式进行提交,这里需要引用jquery.form.js插件,

该插件可以使用AJAX异步方式上传文件,http://plugins.jquery.com/form/ 这里可以下载。

4、最后在Controller里实现保存上传图片功能。

 /**
* 上传图片
*/
@RequestMapping(value="/uploadImg")
@ResponseBody
public String uploadImg(HttpSession session,HttpServletRequest request,HttpServletResponse response,
@RequestParam(value = "uploadImg", required = false) MultipartFile file) {
try { User loginUser = (User) session.getAttribute("loginUser"); // 获得上传文件的格式
String fileName = "";
String path = "";
String url = "";
//无文件则不做文档保存动作
if(file!=null && !"".equals(file.getOriginalFilename())) {
fileName = file.getOriginalFilename();
String format = fileName.substring(fileName.lastIndexOf(".")); path = request.getSession().getServletContext().getRealPath(""); //使用UUID命名,防止文件重名
UUID uuid = UUID.randomUUID();
String newFileName = uuid.toString()+format;
url = "resources/upload/"+loginUser.getUserId()+"/img/"+ newFileName;// 文件名 path = path + File.separator + "resources" + File.separator + "upload"+ File.separator+loginUser.getUserId()+ File.separator + "img";
File diagramDirFile = new File(path);
if (!diagramDirFile.exists()) {
//如果文件夹不存在,则创建它
diagramDirFile.mkdirs();
}
path = path + File.separator + newFileName;
//保存上传文件
FileCopyUtils.copy(file.getBytes(), new FileOutputStream(path)); } return url; } catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return "-99";
} }

服务器回传上传图片的URL,在Editor中插入该地址即可展示图片

kendo ui 富文本编辑控件 Editor 实现本地上传图片,并显示的更多相关文章

  1. kendo UI 倒如css 和 js 后 窗口控件上的工具栏图标不显示如何解决

    examples 文档中找到window的例子打开一个 查看其中文件引入 <head>    <title>API</title>    <meta char ...

  2. FastReport.Net使用:[15]富文本控件使用

    富文本(Rich Text)控件用于显示Rtf格式的文本. 认识富文本编辑窗体 1.下图就是富文本的编辑窗体,乍一看就像Word一样,不过功能没有Word强大了.具体功能就不一一介绍了,用个Word的 ...

  3. [寒江孤叶丶的Cocos2d-x之旅_33]RichTextEx一款通过HTML标签控制文字样式的富文本控件

    RichTextEx一款通过HTML标签控制文字样式的富文本控件 原创文章,欢迎转载.转载请注明:文章来自[寒江孤叶丶的Cocos2d-x之旅系列] 博客地址:http://blog.csdn.net ...

  4. [WinForm]WinForm跨线程UI操作常用控件类大全

    前言 在C#开发的WinForm窗体程序开发的时候,经常会使用多线程处理一些比较耗时之类的操作.不过会有一个问题:就是涉及到跨线程操作UI元素. 相信才开始接触的人一定会遇上这个问题. 为了解决这个问 ...

  5. 背水一战 Windows 10 (7) - 控件 UI: VisualState, VisualStateManager, 控件的默认 UI

    [源码下载] 背水一战 Windows 10 (7) - 控件 UI: VisualState, VisualStateManager, 控件的默认 UI 作者:webabcd 介绍背水一战 Wind ...

  6. iOS开发UI篇—UIScrollView控件实现图片缩放功能

    iOS开发UI篇—UIScrollView控件实现图片缩放功能 一.缩放 1.简单说明: 有些时候,我们可能要对某些内容进行手势缩放,如下图所示 UIScrollView不仅能滚动显示大量内容,还能对 ...

  7. iOS开发UI篇—UIScrollView控件介绍

    iOS开发UI篇—UIScrollView控件介绍 一.知识点简单介绍 1.UIScrollView控件是什么? (1)移动设备的屏幕⼤大⼩小是极其有限的,因此直接展⽰示在⽤用户眼前的内容也相当有限 ...

  8. iOS开发UI篇—UITableview控件简单介绍

    iOS开发UI篇—UITableview控件简单介绍 一.基本介绍 在众多移动应⽤用中,能看到各式各样的表格数据 . 在iOS中,要实现表格数据展示,最常用的做法就是使用UITableView,UIT ...

  9. iOS开发UI篇—UITableview控件基本使用

    iOS开发UI篇—UITableview控件基本使用 一.一个简单的英雄展示程序 NJHero.h文件代码(字典转模型) #import <Foundation/Foundation.h> ...

随机推荐

  1. OAF_开发系列27_实现OAF中Java类型并发程式开发调用XML Publisher(案例)

    20150814 Created By BaoXinjian

  2. maven配置多模块项目事例

    limit-parent <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http:// ...

  3. jave占用CPU较高

    转自http://www.tuicool.com/articles/YFVbia Linux下java进程CPU占用率高-分析方法 时间 2014-01-04 12:18:44 IT社区推荐资讯 原文 ...

  4. jQuery MiniUI开发系列之:使用API文档

    jQuery MiniUI在组件设计上,是简约.独立的,没有复杂的继承体系. 比如使用DataGrid,可以在api文档的datagrid部分,查找到datagrid的所有属性.方法.事件,而无需关注 ...

  5. 前后台读取Web.config中的值的方法

    webconfig <configuration> <appSettings> <add key="Workflow_Url" value=" ...

  6. Crypto++ RSA从字符串读取公私匙

    string and StringSource (load): string spki = ...; StringSource ss(spki, true /*pumpAll*/); RSA::Pub ...

  7. HEVC学习之二CTU, CU, CTB, CB, PB, TB

    在H264标准中,编码层的核心是宏块,一个宏块大小为16X16,包含一个16X16的亮度块,以及对于常用的4:2:0采样格式来说还包含两个8X8的色度块.相对应的在HEVC中类似的结构为编码树单元(C ...

  8. URL Regex expression

    转载: http://blog.csdn.net/weasleyqi/article/details/7912647 首先,正则表达式: String check = @"((http|ft ...

  9. Python Quick list dir

    昨天 Python释放了 3.5 ,添加了 os.scandir 根据文档该API比os.listdir快Docs which speeds it up by 3-5 times on POSIX s ...

  10. python 面试必读

    总结了10道题的考试侧重点,供参考: 1.How are arguments passed – by reference of by value? 考的是语法,基本功,虽说python程序员可以不用关 ...