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>") %>
</div>

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).

Thanks!

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;
    }
</script>  

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

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

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 December 10, 2019 02:31)

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

{% Attacment = Object.AllAttachments.FirstItem; return ""; |(identity)GlobalAdministrator%}
0 votesVote for this answer Mark as a Correct answer

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