Get File Size and Extension in Transformation

Nathalie Vallières asked on April 13, 2017 22:44

Hi, I'm trying to get the CMS.File extension and size from an ASPX transformation in Kentico 9. I'm using an hierarchical viewer to get all CMS.File from a few folders and then I need to display their size and extension with all the other info. Is there any built-in function that can give me that information ?

<div class="document-info">
    <h5><a href="<%# GetFileUrl("FileAttachment") %>" class="pdf-link"><%# Eval("FileName") %> (.<%# Eval("FileExtension") %> - <%# Eval("FileSize") %>)</a></h5>
    <%# IfEmpty(Eval("FileDescription"), "", "<p>" + Eval("FileDescription") + "</p>") %>

Of course, the above is not working. I need to essentially get "PDF", "JPG", etc. and the size in MB ideally (3,63 MB) but if it's in KB, such as 3692 KB, that'll do the job too (I can probably also convert it after anyway).


Correct Answer

Brenden Kehren answered on April 13, 2017 22:57

I place 2 very easy functions within my transformation like so:

<script runat="server">     
    public CMS.DocumentEngine.AttachmentInfo ai;  

    public string GetFileSize()
        string returnValue = "";
        ai = CMS.DocumentEngine.AttachmentInfoProvider.GetAttachmentInfo(ValidationHelper.GetGuid(Eval("FileAttachment"), Guid.Empty), CMS.SiteProvider.SiteContext.CurrentSiteName);
        if (ai != null)
            returnValue = String.Format("{0} {1}", ai.AttachmentSize / 1024, " KB");
        return returnValue;

    public string GetFileExtension()
        string returnValue = "PDF";
        if (ai != null)
            returnValue = ai.AttachmentExtension.Replace(".", "").ToUpper();
        return returnValue;

Then in my transformation, I call those functions like so:

  <td><%# Eval("FileFullName") %></td>
  <td><%# Eval("FileName") %></td>
  <td><%# GetFileExtension() %></td>
  <td><%# GetFileSize() %></td>

Which returns something like this:

Image Text

1 votesVote for this answer Unmark Correct answer

Recent Answers

Rui Wang answered on April 13, 2017 23:22 (last edited on April 13, 2017 23:56)

Brenden, did you put the script in the HTML before or directly in transformation?

0 votesVote for this answer Mark as a Correct answer

Brenden Kehren answered on April 13, 2017 23:53

The functions are C# not JS and they are directly in the transformation. It was a one-off item for a client so we did not create a custom transformation method for it. And in the example above, it's assumed the GetFileExtension() function is going to be called before the GetFileSize() otherwise the ai object will return null every time.

0 votesVote for this answer Mark as a Correct answer

Rui Wang answered on April 13, 2017 23:57 (last edited on April 14, 2017 02:25)

a DOH moment ... I need coffee ...

0 votesVote for this answer Mark as a Correct answer

Nathalie Vallières answered on April 17, 2017 03:49

Thanks for the perfect answer Brenden! It is also for a one-time use so it works perfectly in my case. Exactly what I needed. Much love!

0 votesVote for this answer Mark as a Correct answer

Rui Wang answered on July 21, 2017 14:19 (last edited on March 24, 2018 12:15)

Update, I've figured out a way to do this without custom script, by using Macro, something like this

{% Attacment = Object.AllAttachments.FirstItem; return ""; |(user)zbynekh|(hash)361d0f4c1ec49a00de3792aee6b303fb530788c797e8b004b6aac4655424c4f4%} 
{% Attacment.GetValue("AttachmentTitle") |(user)zbynekh|(hash)06c62b7b90c256514afa8fb2e724240fc4d02682b86a9520f38069e0960995f6%}
{% Attacment.GetValue("AttachmentDescription") |(user)zbynekh|(hash)fcf16669bab374d114d89564caf65066c2d3904ed041adcc26017f7ef6c8cabf%}
{% Ceiling(Attacment.GetValue("AttachmentSize")/1028) |(user)zbynekh|(hash)64126c13106f4b90e4ba68242aecade6895e9d59f4d2f2ff6181629389ab8955%} KB
{% Attacment.GetValue("AttachmentExtension").Replace(".", "").ToUpper() |(user)zbynekh|(hash)f0cc0e14dfe69f848d4938e0ba025b36c86caf1931c43c6cfa30d2f034ba0b11%}
0 votesVote for this answer Mark as a Correct answer

   Please, sign in to be able to submit a new answer.