django-ckeditor上传文件以外的文件

时间:2013-02-20 05:23:19

标签: python django ckeditor

我使用django-ckeditor。

我在图片和Flash上​​传图标中上传图片文件和.swf文件。但是当我上传除这些以外的文件时,我得到的错误是“无法识别图像文件”。对于swf文件,缩略图没有创建,虚拟图像显示没有任何URL。

我在ckeditor中的观点如下: @csrf_exempt

def upload(request):
    upload = request.FILES['upload']
    upload_ext = os.path.splitext(upload.name)[1]
    upload_filename = get_upload_filename(upload.name, request.user)
    out = open(upload_filename, 'wb+')
    for chunk in upload.chunks():
        out.write(chunk)
    out.close()
    if upload_ext != ".swf": 
        create_thumbnail(upload_filename)
    url = get_media_url(upload_filename)
    url = url.replace("\\", "/")
    return HttpResponse("""
    <script type='text/javascript'>
        window.parent.CKEDITOR.tools.callFunction(%s, '%s');
    </script>""" % (request.GET['CKEditorFuncNum'], url))

缩略图,

def create_thumbnail(filename):
    image = Image.open(filename)
    if image.mode not in ('L', 'RGB'):
        image = image.convert('RGB')
   imagefit = ImageOps.fit(image, THUMBNAIL_SIZE, Image.ANTIALIAS)
    imagefit.save(get_thumb_filename(filename))

任何人帮我解决这个问题..

1 个答案:

答案 0 :(得分:2)

从技术上讲,只有图像文件可以有缩略图。所有其他类型应根据其类型具有默认缩略图(可能是图标图像)。在您的代码中,逻辑应该

if upload_ext in [".jpg", ".png",]:
    create_thumbnail(filename)
else:
    create_default_thumbnail(upload_ext) // for .swf, .doc etc