Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Farid Mustafayev 6 posts 117 karma points
    Sep 12, 2018 @ 19:26
    Farid Mustafayev
    0

    File amount limit in the Media Folder

    Hi guys,

    I have a task to limit the amount of the uploaded files in the particular media folders, but I couldn't find any built-in solution for that in Umbraco CMS.

    It should display some message, when user has reached the limit 10 files in the media folder, like "Please delete useless files first" and should prohibit the uploading of the new files.

    Is it possible to implement such things?

    P.S. Umbraco version 7.12.2 assembly: 1.0.6820.12881

  • Veerapandiyan Rajagopal 4 posts 96 karma points
    Sep 13, 2018 @ 04:55
    Veerapandiyan Rajagopal
    0

    I haven't come across such a scenario. However I can give you some directions based on Casper's post on a related query.

    You have to write a custom validation on MediaService.Save event to check for the no of files already in the folder and then showing a message for the user.

    https://our.umbraco.com/forum/using-umbraco-and-getting-started/80916-media-upload-validation#comment-258766

    I will give it try if i get sometime free over this weekend and let you know.

  • Farid Mustafayev 6 posts 117 karma points
    Sep 14, 2018 @ 09:05
    Farid Mustafayev
    0

    Thank you Veerapandiyan,

    I will look at that solution to check the amount of the files in the folder.

    But also would be nice to know the way to display some message user and hide/delete "Upload" button in case when file amount limit was reached.

    Which service can achieve that?

  • Farid Mustafayev 6 posts 117 karma points
    Sep 15, 2018 @ 10:30
    Farid Mustafayev
    100

    Have implemented with the following way:

    public class AppEventHandler : ApplicationEventHandler
    {
        private const int FileAmountLimit = 3;
        private const string MaxAmountReached = "You have reached the Maximum Amount of the files. Please remove other files from the folder before upload.";
    
        /// <inheritdoc />
        protected override void ApplicationStarting(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
        {
            MediaService.Saving += MediaServiceOnSaving;
        }
    
        private void MediaServiceOnSaving(IMediaService sender, SaveEventArgs<IMedia> e)
        {
            var folders = sender.GetByIds(e.SavedEntities.Select(m => m.ParentId));
            if (folders.Any(f => f.Children().Count() >= FileAmountLimit))
            {
                if (e.CanCancel)
                    e.CancelOperation(new EventMessage("File Upload Error", MaxAmountReached, EventMessageType.Error));
            }
        }
    }
    

    And it works like this: MaxAmountReached

Please Sign in or register to post replies

Write your reply to:

Draft