禁止用户将BMP,TIFF等图像上传到Plone中的ImageField [英] Preventing users to upload BMP, TIFF etc. images to ImageField in Plone

查看:83
本文介绍了禁止用户将BMP,TIFF等图像上传到Plone中的ImageField的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

用户这样做是因为他们可以.

The users do it because they can.

但是,图像自动调整大小等会崩溃.

However, image auto-resize etc. breaks down.

这使我成为一个悲伤的男孩.

This make me a sad boy.

如何限制将图片上传到整个站点的GIF,PNG和JPEG?

How to limit image uploads to GIF, PNG and JPEG sitewide?

  • 对于原型

  • For Archetypes

敏捷

推荐答案

这几天我遇到了类似的问题,并像这样解决它们:

i ran into similar problems these days and worked around them like that:

  • 添加一个自定义小部件,该小部件将accept属性添加到文件输入中
  • 设置field.swallowResizeExceptions = True,以便用户至少上传未支持的图像类型时不会出现网站错误
  • 说明在描述中起作用的模仿类型
  • add a custom widget that adds an accept attribute to the file input
  • set field.swallowResizeExceptions = True so users at least don't get a site-error when uploading an unsopported image type
  • state mimetypes that work in description

字段定义如下:

atapi.ImageField('image1',
    swallowResizeExceptions = True,
    widget = atapi.ImageWidget(
        label = _(u"Image 1"),
        description = _(u"Image used in listings. (JPEG, PNG and GIF are supported)"),
        show_content_type = False,
        accept = 'image/*',
        macro = 'mywidgets/myimage',
        ),
    ),

请注意,accept="image/jpeg,image/gif"被firefox11忽略 尽管根据 http://www.w3schools.com/tags/att_input_accept.asp

note that accept="image/jpeg,image/gif"was ignored by firefox11 although it sould be supported according to http://www.w3schools.com/tags/att_input_accept.asp

mywidgets/myimage是原型/skins/widgets/image.pt的定制版本,它使用原型/skins/widgets/file.pt的定制版本

mywidgets/myimage is a customized version of archetypes/skins/widgets/image.pt that uses a customized version of archetypes/skins/widgets/file.pt

<metal:define define-macro="edit">
...
   <metal metal:use-macro="here/mywidgets/myfile/macros/file_upload"/>
...

和mywidgets/myfile.pt只是定义了此宏:

and mywidgets/myfile.pt simply defines this macro:

<metal:define define-macro="file_upload"
       tal:define="unit accessor;
                   size unit/get_size | python:unit and len(unit) or 0;">
    <input type="file"
           size="30"
           tal:attributes="name string:${fieldName}_file;
                           id string:${fieldName}_file;
                           accept widget/accept|nothing;" />
    <script type="text/javascript"
        tal:define="isDisabled python:test(accessor() and size!=0, 'true', 'false')"
            tal:content="string:document.getElementById('${fieldName}_file').disabled=$isDisabled;">
    </script>
</metal:define>

这篇关于禁止用户将BMP,TIFF等图像上传到Plone中的ImageField的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆