- //-----------------------------------------------------------------------
- // <copyright file="AllowExtensionsAttribute.cs" company="None">
- // Copyright (c) Allow to distribute this code and utilize this code for personal or commercial purpose.
- // </copyright>
- // <author>Asma Khalid</author>
- //-----------------------------------------------------------------------
- namespace ImgExtLimit.Helper_Code.Common
- {
- using System;
- using System.Collections.Generic;
- using System.ComponentModel.DataAnnotations;
- using System.Linq;
- using System.Web;
- /// <summary>
- /// File extensions attribute class
- /// </summary>
- [AttributeUsage(AttributeTargets.Field | AttributeTargets.Property, AllowMultiple = false, Inherited = true)]
- public class AllowExtensionsAttribute : ValidationAttribute
- {
- #region Public / Protected Properties
- /// <summary>
- /// Gets or sets extensions property.
- /// </summary>
- public string Extensions { get; set; } = "png,jpg,jpeg,gif";
- #endregion
- #region Is valid method
- /// <summary>
- /// Is valid method.
- /// </summary>
- /// <param name="value">Value parameter</param>
- /// <returns>Returns - true is specify extension matches.</returns>
- public override bool IsValid(object value)
- {
- // Initialization
- HttpPostedFileBase file = value as HttpPostedFileBase;
- bool isValid = true;
- // Settings.
- List<string> allowedExtensions = this.Extensions.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries).ToList();
- // Verification.
- if (file != null)
- {
- // Initialization.
- var fileName = file.FileName;
- // Settings.
- isValid = allowedExtensions.Any(y => fileName.EndsWith(y));
- }
- // Info
- return isValid;
- }
- #endregion
- }
- }
Comments
Post a Comment