富文本编辑的组件有很多,大名鼎鼎的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. 线程优先级抢占实验【RT-Thread学习笔记 3】

    同时处于就绪状态的线程,优先级高的先执行. 高优先级就绪时,低优先级任务让出CPU,让高优先级任务先执行. 创建两个任务函数: //线程优先级抢占 void thread1_entry(void *p ...

  2. Netty的TCP粘包/拆包(源码二)

    假设客户端分别发送了两个数据包D1和D2给服务器,由于服务器端一次读取到的字节数是不确定的,所以可能发生四种情况: 1.服务端分两次读取到了两个独立的数据包,分别是D1和D2,没有粘包和拆包. 2.服 ...

  3. HTML5服务器推送消息的各种解决办法

    摘要 在各种BS架构的应用程序中,往往都希望服务端能够主动地向客户端推送各种消息,以达到类似于邮件.消息.待办事项等通知. 往BS架构本身存在的问题就是,服务器一直采用的是一问一答的机制.这就意味着如 ...

  4. jQuery MiniUI开发系列之:数据验证

    在开发应用系统界面时,往往需要进行很多.复杂的数据验证,当填写的数据符合规定,才能提交保存. jQuery MiniUI提供了比较完美的表单数据验证和错误显示的方式. 常见的表单控件,都有一个验证事件 ...

  5. MYSQL PERFORMANCE_SCHEMA HINTS

    ACCOUNTS NOT PROPERLY CLOSING CONNECTIONS [ 1 ] Works since 5.6 SELECT ess.user, ess.host , (a.total ...

  6. NET映射导致的应用无法访问

    应用环境 IIS 数据库  oracle11G 服务器 windwos2003 企业版 周一早上刚到,听到业务部门的人反应,集团的扫码抢系统登陆不上了,没办法进行出入库操作,大批货物在仓库堆积,承运车 ...

  7. Java使用Mysql数据库实现批量添加数据

    EmployeeDao.java //批处理添加数据 public int saveEmploeeBatch(){ int row = 0; try{ con = DBCon.getConn(); S ...

  8. Python-8 元组tuple

    #1 特殊的列表:元组 元组中的元素不可改变 #2 创建.访问 >>> tuple1=(1,2,3) >>> tuple1=1,2,3 >>> t ...

  9. 使用localResizeIMG微信压缩上传图片安卓报错 weixin://preInjectJSBridge/fail

    微信上传图片是经常使用的功能,首先说一下使用的是:localResizeIMG进行图片压缩上传.感觉还是很好用,基本上功能都能满足. 但是最近在开发这个功能时遇到一个奇怪的问题,就是iphone|ip ...

  10. Java反射机制DOME

    Java反射机制 public class TestHibernate { @Test public void TestHb(){ try { Class cs = Class.forName(&qu ...