0
votes

There's something arcane going on with my custom CKEditor uploader. The image or whatever file I try to upload to the server is correctly uploaded but no matter what I do it's link won't show up in the editor. It looks like the callback to CKEditor in my upload_file.html view doesn't work as it should. The documentation of CKEditor is really sparse about these things, so I could really use some guidance here.

In my controller I have the following upload function:

def upload_file():
    upload = request.vars.upload
    if upload != None:
        if hasattr(upload, 'file'):
            old_filename = upload.filename
            new_filename = db.files.uploaded_data.store(upload.file, upload.filename)
            result = db.files.insert(filename = old_filename,
                                     uploaded_data = new_filename,
                                     created_on = datetime.today())
            if not result:
                message = T('An error has occured during upload.')
                url = ''
            else:
                message = T('File uploaded succesfully.')
                url = URL(r = request, f = 'download', args = new_filename)
            return dict(form = None, cknum = request.vars.CKEditorFuncNum, url = url, message = message)
        else:
            raise HTTP(401, T('Upload is not proper type.'))
    else:
        form = SQLFORM(db.files, fields = ['uploaded_data'])
        upload = request.vars.uploaded_data
        if upload != None:
            form.vars.filename = upload.filename
            form.vars.created_on = datetime.today()
        if form.process().accepted:
            response.flash = T('File uploaded successfully!')
        elif form.errors:
            response.flash = T('form has errors')
        else:
            response.flash = T('please fill out the form')
        return dict(form = clean_form(form))

The view for this function looks like this:

{{if form != None:}}
    {{extend 'layout.html'}}
    {{=form}}
{{else:}}
    <html>
        <body>
            <script type="text/javascript">
                window.opener.CKEDITOR.tools.callFunction({{=cknum}}, '{{=url}}', '{{=message}}');
            </script>
        </body>
    </html>
{{pass}}

I have a test view with a form containing several textareas all of which are properly converted to editors:

{{extend 'layout.html'}}
<script type="text/javascript">
    CKEDITOR.config.filebrowserBrowseUrl = "{{=URL(request.application, c='default', f='upload_file')}}";
    CKEDITOR.config.filebrowserUploadUrl = "{{=URL(request.application, c='default', f='upload_file')}}";
    CKEDITOR.config.filebrowserWindowHeight = '60%';
    CKEDITOR.config.filebrowserWindowWidth = '70%';
</script>
{{=form}}
1

1 Answers

0
votes

I finally found the solution. There's a mistake in the view of the upload_file function.

window.opener.CKEDITOR.tools.callFunction({{=cknum}}, '{{=url}}', '{{=message}}');

should be rewritten to this:

window.parent.CKEDITOR.tools.callFunction({{=cknum}}, '{{=url}}', '{{=message}}');

I copied the first version, which caused me quite a lot of headache, from web2pyslices, so I write this answer here in the hope that it will help others trying to integrate CKEditor with Web2py.