Last active
March 25, 2020 11:44
-
-
Save sandrock/625c585f8245952f6da9c422eb250661 to your computer and use it in GitHub Desktop.
ASP MVC Html.DisplayFileSize helper
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
namespace WhateverNamespaceYouWant | |
{ | |
using System; | |
using System.Collections.Generic; | |
using System.Configuration; | |
using System.Linq; | |
using System.Linq.Expressions; | |
using System.Text; | |
using System.Web; | |
using System.Web.Mvc; | |
using System.Web.Mvc.Html; | |
using System.Web.Routing; | |
public static class HtmlHelperExtensions | |
{ | |
/// <summary> | |
/// Display a human-readable number of bytes with at title for traditionalists. | |
/// </summary> | |
/// <param name="html"></param> | |
/// <param name="length"></param> | |
/// <param name="asHtml"></param> | |
/// <param name="nullValue"></param> | |
/// <returns></returns> | |
public static IHtmlString DisplayFileSize(this HtmlHelper html, long? length, bool asHtml = true, string nullValue = null) | |
{ | |
// see https://en.wikipedia.org/wiki/Binary_prefix | |
// see https://physics.nist.gov/cuu/Units/binary.html | |
if (length != null) | |
{ | |
const long KiB = 1024, MiB = KiB * 1024, GiB = MiB * 1024, TiB = GiB * 1024; | |
const long KB = 1000, MB = KB * 1000, GB = MB * 1000, TB = GB * 1000; | |
string sign = string.Empty; | |
if (length < 0D) | |
{ | |
sign = "-"; | |
length = -length; | |
} | |
double sizeIEC = length.Value; | |
double sizeMetric = length.Value; | |
string suffixIEC, suffixMetric, longSuffixIEC, longSuffixMetric; | |
string format = "{0:F2}"; | |
if (length >= TiB) | |
{ | |
sizeIEC = Math.Round((double)length / TiB, 2); | |
sizeMetric = Math.Round((double)length / TB, 2); | |
suffixIEC = "TiB"; | |
suffixMetric = "TB"; | |
longSuffixIEC = "tebibyte"; | |
longSuffixMetric = "terabyte"; | |
} | |
else if (length >= GiB) | |
{ | |
sizeIEC = Math.Round((double)length / GiB, 2); | |
sizeMetric = Math.Round((double)length / GB, 2); | |
suffixIEC = "GiB"; | |
suffixMetric = "GB"; | |
longSuffixIEC = "gibibyte"; | |
longSuffixMetric = "gigabyte"; | |
} | |
else if (length >= MiB) | |
{ | |
sizeIEC = Math.Round((double)length / MiB, 2); | |
sizeMetric = Math.Round((double)length / MB, 2); | |
suffixIEC = "MiB"; | |
suffixMetric = "MB"; | |
longSuffixIEC = "mebibyte"; | |
longSuffixMetric = "megabyte"; | |
} | |
else if (length >= KiB) | |
{ | |
sizeIEC = Math.Round((double)length / KiB, 2); | |
sizeMetric = Math.Round((double)length / KB, 2); | |
suffixIEC = "KiB"; | |
suffixMetric = "kB"; | |
longSuffixIEC = "kibibyte"; | |
longSuffixMetric = "kilobyte"; | |
} | |
else | |
{ | |
suffixIEC = "B"; | |
suffixMetric = "B"; | |
longSuffixIEC = "byte"; | |
longSuffixMetric = "byte"; | |
format = "{0:F0}"; | |
} | |
var sb = new StringBuilder(); | |
sb.Append("<span class=\"FileSize\" title=\""); | |
sb.Append(sign); | |
sb.AppendFormat(format, sizeIEC); | |
sb.Append(" "); | |
sb.Append(longSuffixIEC); | |
sb.Append(" | "); | |
sb.Append(sign); | |
sb.AppendFormat(format, sizeMetric); | |
sb.Append(" "); | |
sb.Append(longSuffixMetric); | |
if (length.Value > KiB) | |
{ | |
sb.Append(" | "); | |
sb.Append(sign); | |
sb.Append(length.Value); | |
sb.Append(" bytes"); | |
} | |
sb.Append("\">"); | |
sb.Append(sign); | |
sb.AppendFormat(format, sizeIEC); | |
sb.Append(" "); | |
sb.Append("<abbr title=\""); | |
sb.Append(longSuffixIEC); | |
sb.Append("\">"); | |
sb.Append(suffixIEC); | |
sb.Append("</abbr>"); | |
sb.Append("</span>"); | |
return MvcHtmlString.Create(sb.ToString()); | |
} | |
else | |
{ | |
return MvcHtmlString.Create(nullValue); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
It looks like this: