Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • syn-rg 282 posts 425 karma points
    Feb 27, 2014 @ 07:07
    syn-rg
    0

    Display Media items with Date, File size, and Extension

    I'm trying to displaying the Media items, to include the date, file size, and extension.

    However, I need a variable for the file size, so it display as either "KB" or "MB" etc.

    This is my current code, which just displays the umbracoBytes as it's numerical value:

    @helper RenderMediaResult(SearchViewModel model, IPublishedContent result)
    {
        <div class="ezsearch-result">
            <h2><a href="@(result.GetPropertyValue<string>("umbracoFile"))" class="@(result.GetPropertyValue<string>("umbracoExtension"))">@result.Name</a></h2>
    
            <p class="file-info">@(result.CreateDate.ToString("dd MMM yyyy")), @(result.GetPropertyValue<string>("umbracoBytes")) @(result.GetPropertyValue<string>("umbracoExtension"))</p>
    
            @foreach (var field in model.PreviewFields.Where(field => result.HasValue(field)))
            {
                <p>@Highlight(Truncate(Umbraco.StripHtml(result.GetPropertyValue(field).ToString()), model.PreviewLength), model.SearchTerms)</p>
                break;
            }
        </div>
    }
    

    I'd really appreciate some assistance with formatting the "umbracoBytes"

    Cheers, JV

  • Dennis Aaen 4500 posts 18255 karma points admin hq c-trib
    Feb 27, 2014 @ 08:36
    Dennis Aaen
    103

    Hi JV,

    Maybe you could do someting like this.

    @helper RenderMediaResult(SearchViewModel model, IPublishedContent result)
    {
        var bytes = Convert.ToInt64(@result.GetPropertyValue<string>("umbracoBytes"));
        var x = ((Math.Round(Convert.ToDecimal(@bytes) / 1048576, 1)).ToString() + "MB");
        var y = ((Math.Round(Convert.ToDecimal(@bytes) / 1024, 1)).ToString() + "KB");
        var z = (bytes >= 1048576)? x:y;

        <div class="ezsearch-result">
            <h2><a href="@(result.GetPropertyValue<string>("umbracoFile"))" class="@(result.GetPropertyValue<string>("umbracoExtension"))">@result.Name</a></h2>

            <p class="file-info">@(result.CreateDate.ToString("dd MMM yyyy")), @z @(result.GetPropertyValue<string>("umbracoExtension"))</p>

            @foreach (var field in model.PreviewFields.Where(field => result.HasValue(field)))
            {
                <p>@Highlight(Truncate(Umbraco.StripHtml(result.GetPropertyValue(field).ToString()), model.PreviewLength), model.SearchTerms)</p>
                break;
            }
        </div>
    }

    I foud the inspiration for the code here: http://our.umbraco.org/forum/developers/razor/48326-Converting-to-Int64-Failed

    Hope this can help you.

    /Dennis

Please Sign in or register to post replies

Write your reply to:

Draft