限制umbraco媒体纠察队的上传大小和文件类型

限制umbraco媒体纠察队的上传大小和文件类型,umbraco,umbraco7,Umbraco,Umbraco7,尝试通过dialogService选择媒体,如下所示 dialogService.mediaPicker({ startNodeId: undefined, multiPicker: false, cropSize: undefined, showDetails: true, callback: function (data) { } 是否有办

尝试通过dialogService选择媒体,如下所示

  dialogService.mediaPicker({
            startNodeId: undefined,
            multiPicker: false,
            cropSize: undefined,
            showDetails: true,
            callback: function (data) {

             }

是否有办法自定义此媒体选择器配置,只允许某些媒体类型并指定大小

我认为您最好的选择是为MediaService的保存事件创建一个事件处理程序,并在保存文件之前检查您的条件

更多信息请点击此处:

public类RestrictMediaTypeAndSize:ApplicationEventHandler
{
受保护的覆盖无效应用程序已启动(UmbracoApplicationBase umbracoApplication、ApplicationContext ApplicationContext)
{
MediaService.Saving+=MediaService\u Saving;
}
私有void MediaService_保存(IMediaService发送方,Umbraco.Core.Events.SaveEventArgs e)
{
foreach(e.SavedEntities中的var媒体)
{
//您可以检查其他类型的内容
if(media.ContentType.Alias==Image.ModelTypeAlias)
{
var fileExtension=GetFileExtension(媒体);
如果(!HasAllowedExtension(fileExtension))
{
//我们取消了保存过程
e、 取消=真;
//我们向用户发送消息。
var errorMessage=new EventMessage(“错误”,“不允许使用{fileExtension}类型的$”文件。”);
e、 Messages.Add(errorMessage);
//我们发现一个错误,因此退出foreach循环以完成执行。
打破
}
如果(!HasValidDimensions(媒体))
{
//我们取消了保存过程
e、 取消=真;
//我们向用户发送消息。
var errorMessage=new EventMessage(“错误”,“不允许大小为(无论您的限制如何)的文件”);
e、 Messages.Add(errorMessage);
//我们发现一个错误,因此退出foreach循环以完成执行。
打破
}
}
}
}
private bool HasAllowedExtension(字符串文件扩展名)
{
字符串[]allowedExtensions=新字符串[]{.jpg“,.png”};
返回allowedExtensions.Contains(文件扩展名);
}
私人bool HasValidDimensions(IMedia媒体)
{
//获取上载文件时填充的属性的图像尺寸。
变量高度=(int)media.Properties[“umbracoHeight”].Value;
var width=(int)media.Properties[“umbracoWidth”].Value;
//检查你想要的任何条件。
返回高度<1000,返回宽度<2000;
}
私有字符串GetFileExtension(IMedia媒体)
{
//umbracoFile属性是一个Json对象,我们需要它的src属性,即文件路径。
var filePath=JObject.Parse(media.Properties[“umbracoFile”].Value.ToString()).SelectToken(“$.src”).Value();
返回System.IO.Path.GetExtension(filePath);
}
}