November Happy Hour will be moved to Thursday December 5th.

restrict file type for ContentReference property

Vote:
 

Is there a way to only allow specific file types in the ContentReference property? For example below, I only want to allow JPG and PNG, no GIFs (even though GIFs are allowed elsewhere on the site.

        [CultureSpecific]
        [AllowedTypes(typeof(ImageFile))]
        [Display(
            Name = "Image",
            GroupName = SystemTabNames.Content,
            Order = 30)]
        public virtual ContentReference Poster { get; set; }



[Pasting files is not allowed]

#173293
Dec 19, 2016 20:54
Vote:
 

What if you create an ImageData class for each filetype, then set the AllowedTypes to use those classes?

Start with a base class, for all image-related properties:

    public abstract class BaseImageData : ImageData
    {
        public virtual string AltText { get; set; }
    }

Create image classes for each extension type:

    [ContentType(GUID = "3016768f-180c-425c-97f7-e0a3dcbef9b7")]
    [MediaDescriptor(ExtensionString = "jpg")]
    public class JpgImageFile : BaseImageData
    {
    }

    [ContentType(GUID = "06e55dad-95da-44c5-a551-3cfbc820d853")]
    [MediaDescriptor(ExtensionString = "gif")]
    public class GifImageFile : BaseImageData
    {
    }

    [ContentType(GUID = "e476f5c3-d2d6-4659-9134-14c3c8b7efd3")]
    [MediaDescriptor(ExtensionString = "png")]
    public class PngImageFile : BaseImageData
    {
    }

Then use those classes in the AllowedTypes:

    [ContentType(GUID = "d7a0797e-241a-410e-b35e-bb600aefec9b")]
    public class StandardPage : PageData
    {
        [AllowedTypes(new[] { typeof(JpgImageFile), typeof(PngImageFile) })]
        public virtual ContentReference Poster { get; set; }
    }
#173295
Edited, Dec 20, 2016 5:49
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.