阻止用户将PMP,TIFF等图像上传到Plone中的ImageField

时间:2012-02-03 11:04:21

标签: plone archetypes dexterity

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

然而,图像自动调整大小等会中断。

这让我成为一个伤心的男孩。

如何限制图片上传到全网站的GIF,PNG和JPEG?

  • 对于Archetypes

  • Forxterity

3 个答案:

答案 0 :(得分:5)

使用Archetypes可以覆盖图像内容类,或使用以下模式创建自己的自定义图像内容类。

你可以添加一行

allowable_content_types = ('image/gif', 'image/jpeg', 'image/png'),

到您的架构

MyImageSchema = schemata.ATContentTypeSchema.copy() + atapi.Schema((
        ImageField('image',
            required = False,
            allowable_content_types = ('image/gif', 'image/jpeg', 'image/png'),
            storage=AttributeStorage(),
            sizes= {'large'   : (768, 768),
                   'preview' : (400, 400),
                   'mini'    : (200, 200),
                   'thumb'   : (128, 128),
                   'tile'    :  (64, 64),
                   'icon'    :  (32, 32),
                   'listing' :  (16, 16),
                  },
          widget = ImageWidget(
                     label=_(u"Image"),
                     show_content_type=False,
             ),
    ),

我可能会使用模式扩展器来扩展Image类,覆盖该特定字段

http://weblion.psu.edu/services/documentation/developing-for-plone/products-from-scratch/schemaextender

答案 1 :(得分:1)

这些天我遇到了类似的问题并且像这样解决了这些问题:

  • 添加自定义窗口小部件,将accept属性添加到文件输入
  • 设置field.swallowResizeExceptions = True,以便用户在上传不支持的图片类型时至少不会出现网站错误
  • 状态描述中的mimetypes

字段定义如下所示:

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

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

支持它

mywidgets / myimage是原型/皮肤/小部件/ image.pt的自定义版本,它使用原型/皮肤/小部件/ file.pt的自定义版本

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

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

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

答案 2 :(得分:0)

使用后验证事件对AT进行侧面限制:

检查How to restrict image file extension on Plone?