Copied to clipboard

Flag this post as spam?

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


  • Fuji Kusaka 2203 posts 4220 karma points
    May 03, 2012 @ 13:24
    Fuji Kusaka
    0

    UmbracoBytes

    HI All,

    Can someone explain how to display fileSize in Razor??

    Same as you would do in XSLT.

     <xsl:variable name="fileSize" select="umbracoBytes"/>
              <xsl:choose>
              <xsl:when test="$fileSize &gt;= 1048576">
                <xsl:value-of select="round($fileSize div 1048576)"/> MB              
              </xsl:when>
              <xsl:when test="$fileSize &gt;= 1024">
                <xsl:value-of select="round($fileSize div 1024)"/> KB            
              </xsl:when>
            <xsl:otherwise>
                 <xsl:text>0 Bytes</xsl:text>
            </xsl:otherwise>
        </xsl:choose>        

    Any Suggestions?

    //fuji

  • gilad 185 posts 425 karma points
    May 03, 2012 @ 13:42
    gilad
    0

    Hi.

    You can get the fileSize  in Bytes with:

    @Model.Media("image","umbracoBytes")

    so.. it could be something like this:

    @{

    int bytes = int.Parse(Model.Media("image","umbracoBytes"));
    int inMB =  
    bytes / 1048576
    int inKB = bytes / 1024

    }

    Hope that help. cheers

  • Fuji Kusaka 2203 posts 4220 karma points
    May 06, 2012 @ 14:16
    Fuji Kusaka
    0

    Hi Gilad,

    Thanks for the reply but nope didnt get this working.  This is how my codes looks like atm

      var chosenMedia Model.MediaById(1125);
         var items 10;
            
         

         if  (chosenMedia.NodeTypeAlias  ==  "Folder")
      
         {
             foreach(var pdf in chosenMedia.Children.OrderBy("id desc").Take(items))
             {
                 if  (pdf.NodeTypeAlias  ==  "File")
                 {
                             
                     <li><href="@pdf.umbracoFile" alt="@pdf.Name" target="_blank">@pdf.Name</a&nbsp@pdf.umbracoBytes  </li>
                
                 }          
             
             }

    Am getting the right fileSize but somehow i need to round it up to KB or MB

  • Dirk Maes 6 posts 26 karma points
    Jul 10, 2012 @ 12:42
    Dirk Maes
    0

    I had the same problem.  Int.Parse doesn't seem to work, but this does:

      var x = ((Convert.ToInt64(@file.umbracoBytes) / 1024).ToString() + " Kb");
     <p>Download: <a href="@file.umbracoFile" target="_blank">@x</a></p>

  • Fuji Kusaka 2203 posts 4220 karma points
    Jul 19, 2012 @ 15:49
    Fuji Kusaka
    0

    Hi Dirk,

    Thanks for the response. I can't figure out however how to display either KB or MB.

     var bytes @file.umbracoBytes;
                         var ((Convert.ToInt64(@file.umbracoBytes1024).ToString(" KB");
                         var ((Convert.ToInt64(@filef.umbracoBytes1048576).ToString(" MB");

                  if(@bytes >= 1024 ){
                               <li>@pdf.Name @x</a></li>
                         }
    else  if(@bytes >=  1048576 ){
                               <li>@pdf.Name @y</a></li>
                         }

    Any suggestion ??

     

  • Scott Williams 14 posts 71 karma points
    Jul 19, 2012 @ 16:12
    Scott Williams
    1

     

    var bytes = @file.umbracoBytes;

    should read

    var bytes = Convert.ToInt64(@file.umbracoBytes);

    for your code to work (also you can skip all the @varname all over the place)

    so it should end up reading 

     

    @{
        var bytes = Convert.ToInt64(file.umbracoBytes);

        if(bytes >= 1024 ){
            <li>@pdf.Name @(byte / 1024)KB</a></li>
        }
        else if(bytes >=  1048576 ){
            <li>@pdf.Name @(byte / 1048576)MB</a></li>
        }
    }

     

  • Dirk Maes 6 posts 26 karma points
    Jul 19, 2012 @ 16:12
    Dirk Maes
    1
      var x = "";
            if (Convert.ToUInt64(@file.umbracoBytes) < 1048576)
            {
                x = ((Math.Round(Convert.ToDecimal(@file.umbracoBytes) / 1024, 0)).ToString() + " Kb");
            }
            else
            {
                x = ((Math.Round(Convert.ToDecimal(@file.umbracoBytes) / 1048576, 2)).ToString() + " Mb");
            }
    <li>@Dictionary[@umbraco.library.GetPreValueAsString(@b.taal)] - pdf - <a href="@file.umbracoFile" target="_blank">@x</a></li>

     

    Another improvement: using the "round" function and decimals to get a more accurate estimation of the file-size.

  • Fuji Kusaka 2203 posts 4220 karma points
    Jul 19, 2012 @ 16:34
    Fuji Kusaka
    0

    Thanks to both of you for the response @scott @Dirk.

    However i have combined both solution, converting most of xslt to razor file!!

    //fuji

  • Brandon Furtado 2 posts 72 karma points
    Sep 11, 2024 @ 05:45
    Brandon Furtado
    0

    You can use the linkSize variable where ever you need to print the doc size

    var linkSize = "";
    
     var bytes = @Model.Value("umbracoBytes").ToInt();
    
     var kbValue = ((Convert.ToInt64(bytes) / 1024).ToString() + " KB");
     var mbValue = ((Convert.ToInt64(bytes) / 1048576).ToString() + " MB");
      if(bytes >= 1024 ){
         linkSize = kbValue;
     }
     else  if(bytes >=  1048576 ){
         linkSize = mbValue;
     }
    
Please Sign in or register to post replies

Write your reply to:

Draft