NicEdit + image upload on a textarea

2,069 views
Skip to first unread message

Loïc

unread,
Feb 22, 2013, 10:20:53 AM2/22/13
to web...@googlegroups.com
Dear all,

I had some trouble to use the "upload" button in NicEdit WYSIWYG editor.
Now it works, so I share an example with wou, hoping it can help somebody. This code is probably not optimized, so feel free to adapt it

This code uploads images in static/images/pages_content/[Name of the page]/

In a view containing a textarea field add :
<script type="text/javascript" src="{{=URL('static', 'nicEdit/nicEditComplete.js')}}"></script>
<script type="text/javascript">
    bkLib
.onDomLoaded(function() {
       
new nicEditors.allTextAreas({
            iconsPath
:"{{=URL('static', 'nicEdit/nicEditorIcons.gif')}}",
            buttonList
: ['bold','italic','underline','strikeThrough','left', 'center',
               
'right', 'justify', 'ol', 'ul', 'fontSize', 'fontFormat', 'indent', 'outdent', 'upload'],
            uploadURI
: "{{=URL('default', 'nicedit_image_upload', args=page.url)}}",
       
})
   
});
</script>

In your controller :

def nicedit_image_upload():
   
"""
    Controller to upload images with nicedit
    """

   
from gluon.contrib.simplejson import dumps
   
import os
   
    page_url
= request.args[0]
    pathname
= os.path.join(request.folder,'static','images', 'pages_content', page_url)
   
if not os.path.exists(pathname):
        os
.mkdir(pathname)

    pathfilename
= os.path.join(pathname, request.vars.image.filename)
    dest_file
= open(pathfilename, 'wb')
   
   
try:
        dest_file
.write(request.vars.image.file.read())
   
finally:
        dest_file
.close()

   
#Make a thumbnail (max 600*600px) of the uploaded Image
   
try:
       
from PIL import Image
        im
= Image.open(pathfilename)
        im
.thumbnail((600,600),Image.ANTIALIAS)
        im
.save(pathfilename)
   
except:
       
pass
    links_dict
= {"original":URL('static', 'images/pages_content/'+page_url+'/'+request.vars.image.filename)}
    set_dict
= {"links" : links_dict}
    upload_dict
= {"upload" : set_dict}

   
return dumps(upload_dict)



Reply all
Reply to author
Forward
0 new messages