Skip to content

Instantly share code, notes, and snippets.

@islaytitans

islaytitans/FileSizeCheck.cs Secret

Created Jul 1, 2015
Embed
What would you like to do?
Check file size of an uploaded file
private const int DefaultFileSizeLimit = 3000000;
private int? _fileSizeLimitInBytes;
private int FileSizeLimitInBytes
{
get
{
if (_fileSizeLimitInBytes == null || _fileSizeLimitInBytes == DefaultFileSizeLimit)
{
_fileSizeLimitInBytes = DefaultFileSizeLimit;
Item fileUploadConfig = ItemNodes.SiteConfig.Children.FirstOrDefault(x => x.TemplateID == Enumerators.SitecoreConfig.Guids.Templates.FileUploadConfigId);
if (fileUploadConfig != null && fileUploadConfig.Fields[Enumerators.SitecoreConfig.Fields.Global.ImageFileSizeLimit] != null
&& !string.IsNullOrEmpty(fileUploadConfig[Enumerators.SitecoreConfig.Fields.Global.ImageFileSizeLimit]))
{
int sizeInMegaBytes;
bool success =
int.TryParse(fileUploadConfig[Enumerators.SitecoreConfig.Fields.Global.ImageFileSizeLimit],
out sizeInMegaBytes);
if (success)
_fileSizeLimitInBytes = sizeInMegaBytes * 1000000;
}
}
return _fileSizeLimitInBytes.Value;
}
}
private bool ValidateFileSize(HttpPostedFile postedFile)
{
var sizeInBytes = postedFile.ContentLength;
return (sizeInBytes <= FileSizeLimitInBytes);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment