Skip to content

Instantly share code, notes, and snippets.

@egandalf
Created September 30, 2016 19:52
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save egandalf/238d477997da285542a86f9af5c9dc16 to your computer and use it in GitHub Desktop.
Save egandalf/238d477997da285542a86f9af5c9dc16 to your computer and use it in GitHub Desktop.
Set maximum upload limit on images in Episerver
using EPiServer.Framework;
using EPiServer.Framework.Initialization;
using EPiServer.Core;
using EPiServer.ServiceLocation;
using Alloy.Models.Media;
using EPiServer;
namespace Alloy.Business.Initialization
{
[InitializableModule]
[ModuleDependency(typeof(EPiServer.Web.InitializationModule))]
public class FileSizeValidationInitializationModule : IInitializableModule
{
private IContentEvents contentEvents => ServiceLocator.Current.GetInstance<IContentEvents>();
private IContentRepository contentRepository => ServiceLocator.Current.GetInstance<IContentRepository>();
public void Initialize(InitializationEngine context)
{
contentEvents.CreatingContent += ContentEvents_CreatingContent;
}
private void ContentEvents_CreatingContent(object sender, EPiServer.ContentEventArgs e)
{
if (e.Content is ImageFile)
{
var imageFile = e.Content as ImageFile;
using (var blobData = imageFile.BinaryData.OpenRead())
{
if (blobData.Length > 1048576)
{
e.CancelAction = true;
e.CancelReason = "File size exceeds the maximum allowed (1 MB).";
}
}
}
}
public void Uninitialize(InitializationEngine context)
{
//Add uninitialization logic
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment