使用CKeditor 上传图片

要实现的效果

 

 

http://yitong.xiaodoutao.com/

 

 

1.---------------------------------------

 

找到ckeditor---->plugins---->image---->dialogs---->image.js文件

 

 

2.----------------------------------------

 

找到validate:CKEDITOR.dialog.validate.notEmpty(b.lang.image.urlMissing)},然后在后面添加

{type:'button',id:'myUpload',style:'display:inline-block;margin-top:10px;',align:'center',label:'新上传',onClick:function(){var retFile=showModalDialog('../common/uploadImage.jsp','','dialogHeight:20;dialogWidth:29;');if(retFile != null){this.getDialog().setValueOf('info','txtUrl',retFile);}}},

 

 

3.新建uploadImage.jsp----------------使用jquery.ajaxfileupload.js异步上传图片--------------------------------

 

<script type="text/javascript" src="/common/js/jquery.ajaxfileupload.js"></script>

<script type="text/javascript">

$(function(){

$("#submit").click(function(){

 

var imgPath = document.getElementById("boy").value;

//alert(imgPath);

if(imgPath == null || imgPath == "")

{

alert("请浏览图片");

return false;

};

 

$.ajaxFileUpload({

 

async : false,

cache:false,

type: 'POST',

dataType : "text",

fileElementId:'boy',    //file的Id

url: "${ctx}/downloadCenter/saveImage.do",//请求的action路径

 

error: function () {//请求失败处理函数

alert('请求失败'); },

 

success:function(data){ //请求成功后处理函数。

//alert(data);

window.returnValue=data; //返回父页面

window.close();

}

})

});

});

 

</script>

 

</head>

 

<body>

 

<form action="" enctype="multipart/form-data" method="post" name="myform"> <br/>

<strong> <P>选择要上传的图片:</P></strong>

图片: <INPUT type="file" id="boy" name="file"> <BR><br/>

<INPUT type="button" value="提交" id="submit">&nbsp;&nbsp;<INPUT type="reset" value="取消"> </form>

</body>

</html>

 

上传所需要的action-----------------------------------------------------------------------------------------------------

 

/** * CKedit插件上传图片所使用的方法 * @throws IOException */ public void saveImage() throws IOException{ log.debug("dao.DownloadCenterAction.class method save : start"); DownloadCenter downloadCenter = new DownloadCenter(); String root = ServletActionContext.getRequest().getRealPath("upload"); HttpServletRequest request = ServletActionContext.getRequest (); String path = request.getContextPath(); String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/"; File dir = new File(root); if (dir.exists()==false){ dir.mkdir(); } try { for(int i = 0;i < file.size(); i++){ InputStream is = new FileInputStream(file.get(i)); File destFile = new File(root,this.getFileFileName().get(i)); OutputStream os = new FileOutputStream(destFile); byte[] buffer = new byte[1024]; int length = 0; while((length= is.read(buffer)) > 0){ os.write(buffer, 0, length); } is.close(); os.close(); } if (fileFileName != null || !fileFileName.equals("")) { for (int j = 0; j < fileFileName.size(); j++) { basePath = basePath+"upload/"+fileFileName.get(j); } } } catch (Exception e) {ss e.printStackTrace(); System.out.println("添加上传文件失败"); }finally{ System.err.println(basePath); getResponse().getWriter().print(basePath); log.debug("dao.DownloadCenterAction.class method save : end"); } }

http://yitong.xiaodoutao.com/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值