-
-
Save islaytitans/385ca05ceca1aed31cd2 to your computer and use it in GitHub Desktop.
Check file size of an uploaded file
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
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