主题: Grails使用kindeditor,支持附件
作者: rox, 发布日期: 2011-07-21 00:30:49, 浏览数: 3753

1、将附件 grails-kindeditor.rar 带目录解压到web-app目录

2、gsp文件代码:

 

Html代码 
  1. <head>  
  2. <script charset="utf-8" src="${resource(dir: 'kindeditor', file: 'kindeditor.js')}"></script>  
  3. <script>  
  4.     KE.show({  
  5.         id : 'repost_content',  
  6.         resizeMode : 1,  
  7.         allowPreviewEmoticons : false,  
  8.         allowUpload : true,  
  9.         imageUploadJson : '${createLink(action:'uploadimagejson')}',  
  10.         attachUploadJson : '${createLink(action:'uploadattachjson')}',  
  11.         items : [  
  12.             'fontname', 'fontsize', '|', 'textcolor', 'bgcolor', 'bold', 'italic', 'underline',  
  13.             'removeformat', '|', 'justifyleft', 'justifycenter', 'justifyright', 'insertorderedlist',  
  14.             'insertunorderedlist', '|', 'emoticons', 'image', 'attach', 'flash', 'link']  
  15.     });  
  16. </script>  
  17. </head>  
  18. <body>  
  19. <g:textArea id="repost_content" name="content" rows="500" cols="400"/>  
  20. </body>  

 

 

3、action代码:

 

本来是使用对象封装了文件部分属性,这里假定Attach对象有originalName属性,再使用最简单的saveFile,getFile闭包实现

 

Java代码 
  1. import grails.converters.JSON  
  2.   
  3. def uploadattachjson = {  
  4.     def text = [error: 1, message: '请选择文件。'] as JSON  
  5.     if (params.attachFile) {  
  6.         def attach = savePic(params.attachFile)  
  7.         text =  [error: 0, url: createLink(action: 'downattach', id: attach.id)] as JSON  
  8.     }  
  9.   
  10.     render(contentType: 'text/html; charset=UTF-8', text: text)  
  11. }  
  12.   
  13. def downattach = {  
  14.     def attach = getFile(params.id)  
  15.     if (attach.exists()) {  
  16.         response.setContentType("application/octet-stream")  
  17.         response.setHeader("Content-disposition""attachment;filename=${getFileName(attach.originalFilename)}" )  
  18.         response.setContentLength(attach.length() as int)  
  19.         response.outputStream.write(attach.bytes)  
  20.     } else {  
  21.         response.sendError(404)  
  22.     }  
  23.   
  24. }  
  25.   
  26. def getFileName = {  
  27.     String agent = request.getHeader('USER-AGENT')  
  28.     if (null != agent && -1 != agent.indexOf('MSIE')) {  
  29.         URLEncoder.encode(it, 'UTF8')  
  30.     } else {  
  31.         MimeUtility.encodeText(it, 'UTF8''B')  
  32.     }  
  33. }  
  34.   
  35. def saveFile = {  
  36.     def attach = new Attach()  
  37.     attach.originalName = it.originalFilename  
  38.   
  39.     it.transferTo(  
  40.         new File('c:\attach\'+it.originalFilename)  
  41.     )  
  42.     attach.save()  
  43. }  
  44.   
  45. def getFile = {  
  46.     def attach = Attach.get(it)  
  47.     new File('c:\attach\'+attach.originalName)  
  48. }  
  

 

 

附件见原帖:

http://rox.iteye.com/blog/1129290

 

 感谢知菋的文章,帮助很大。

http://dxz506.blog.163.com/blog/static/103254696201102153911318/

作者: Roddy, 发布日期: 2011-07-21 20:12:31
谢谢分享,Grails语法好简洁。
回复
作者: rox, 发布日期: 2011-07-23 00:52:58

回复Roddy:

不客气,呵呵!

Grails本来就是Java平台下Rails的参考实现。

上次发帖,有些代码省略了,现已补上。 

回复
发表新帖 发表回复