0

我正在使用 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))

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

4

1 回答 1

2

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

if upload_ext in [".jpg", ".png",]:
    create_thumbnail(filename)
else:
    create_default_thumbnail(upload_ext) // for .swf, .doc etc
于 2013-02-20T05:36:40.630 回答