无法在web2py中使用自制的CKEditor文件上传器

时间:2012-07-05 17:39:10

标签: ckeditor web2py

我的自定义CKEditor上传器出现了一些神秘的事情。我尝试上传到服务器的图像或任何文件都已正确上传,但无论我做什么,它的链接都不会显示在编辑器中。看起来我的upload_file.html视图中对CKEditor的回调不能正常工作。 CKEditor的文档对这些事情非常稀疏,所以我可以在这里使用一些指导。

在我的控制器中,我有以下上传功能:

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))

此功能的视图如下所示:

{{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}}

我有一个包含多个textareas的表单的测试视图,所有这些都正确地转换为编辑器:

{{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 个答案:

答案 0 :(得分:0)

我终于找到了解决方案。在upload_file函数的视图中存在错误。

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

应该改写为:

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

我复制了第一个版本,这引起了我很多头疼,来自web2pyslices,所以我在这里写这个答案,希望它能帮助其他人尝试将CKEditor与Web2py集成。