Skip to content

Instantly share code, notes, and snippets.

@islaytitans
Created July 1, 2015 16:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save islaytitans/385ca05ceca1aed31cd2 to your computer and use it in GitHub Desktop.
Save islaytitans/385ca05ceca1aed31cd2 to your computer and use it in GitHub Desktop.
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