Is there a way to check if the request is coming from Browse for media files
Or is it coming upload file
or upload files
I have code to check upload image size
public class ImageSizeCheck : UploadProcessor
{
public List<string> RestrictedExtensions { get; set; }
public ImageSizeCheck()
{
RestrictedExtensions = new List<string>();
}
public void Process(UploadArgs args)
{
Assert.ArgumentNotNull((object)args, "args");
if (args.Destination == UploadDestination.File)
return;
foreach (string index in args.Files)
{
HttpPostedFile file = args.Files[index];
if (!string.IsNullOrEmpty(file.FileName) && IsRestrictedExtension(file.FileName))
{
if ((long)file.ContentLength > MaxImageSizeInDatabase)
{
String message = string.Format("The image {0} can not be uploaded. The maximum size should be {1}.", file.FileName, MainUtil.FormatSize(MaxImageSizeInDatabase));
args.ErrorText = message;
Log.Warn(args.ErrorText, this);
message = "\"" + message + "\"";
string str = "<script>alert(" + message + ")</script>";
HttpContext.Current.Response.Write(str);
args.AbortPipeline();
break;
}
}
}
}
private bool IsRestrictedExtension(string filename)
{
return RestrictedExtensions.Exists(restrictedExtension => string.Equals(restrictedExtension, Path.GetExtension(filename), StringComparison.CurrentCultureIgnoreCase));
}
public static long MaxImageSizeInDatabase
{
get
{
return Sitecore.Configuration.Settings.GetLongSetting("Media.MaxImageSizeInDatabase", 524288000L);
}
}
}
The alert is working for UploadFile
or UploadFiles
but it is giving error for Browse for media files
.
SyntaxError: Unexpected token < in JSON at position 0
If I find that if the request is coming from Browse for media files
then I can skip HttpContext.Current.Response.Write(str);
Any suggestion would be appreciated. Thanks in advance.