Created
September 30, 2016 19:52
-
-
Save egandalf/238d477997da285542a86f9af5c9dc16 to your computer and use it in GitHub Desktop.
Set maximum upload limit on images in Episerver
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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