Display File Size in ASCX transformation.

Mike Bilz asked on March 2, 2017 01:36

Hello Kentico Team,

I have created a Repeater that displays a list of CMS.file pages. I was able to get the link, description, and file type to display correctly, but I cannot seem to get the file size to work.

I have tried two different methods. The first:

<%# DataHelper.FileSizeFormat(CMS.DocumentEngine.AttachmentInfoProvider.GetAttachmentInfo(Eval("FileGUID"), Eval("NodeSiteID")).AttachmentSize) %>

Which I cobbled together from other discussions of this issue, errors out. Giving me this less than helpful explanation: "'CMS.DocumentEngine.AttachmentInfoProvider.GetAttachmentInfo(int, System.Guid)' has some invalid arguments"

The second method, which is more straight forward:

<%# DataHelper.GetSizeString(ValidationHelper.GetLong(Eval("FileSize"), 0)) %>

Returns "0 B" every time.

This second method seems like what I should be using, but I can't find a working replacement for the Eval("FileSize") argument.

I would love to be able to display the file size in kB if possible.

Any assistance would be greatly appreciated. Thanks in advance.

-mike

Correct Answer

Peter Mogilnitski answered on March 2, 2017 02:30

<script runat="server">
   string sizeInKb;

   protected override void OnInit(EventArgs e)
   {
       CMS.DocumentEngine.AttachmentInfo ai = CMS.DocumentEngine.AttachmentInfoProvider.GetAttachmentInfo(ValidationHelper.GetGuid(Eval("FileAttachment").ToString(), Guid.Empty), CMS.SiteProvider.SiteContext.CurrentSiteName);
       sizeInKb =  string.Format("{0:n1} KB", ai.AttachmentSize/ 1024f);
    }
</script>  
<%#Eval("FileName")%> (<%#sizeInKb%>)
0 votesVote for this answer Unmark Correct answer

Recent Answers


Mike Bilz answered on March 2, 2017 17:58

Thanks Peter!

That worked perfectly. Though I am a little surprised that this isn't easier to do.

-mike

0 votesVote for this answer Mark as a Correct answer

Peter Mogilnitski answered on March 2, 2017 19:18

Mark,

Agreed, yeah it is kinda awkward.

The problem is that the CMS.File is represented by content_file table in SQL and this table doesn't have the attachment size, binary and other columns, all this data is in the CMS_attachment table. When you run your repeater for CMS.File type it means you are accessing content_file table. Ideally you would need JOIN between the two: select attachmentsize, * from CMS_attachment join content_file on FileAttachment = attachmentguid

One way to solve this awkwardness is to use a repeater with a query or you can use Attachments, for example to show attachments of your current document you can do with a simple macro:

{% foreach (attachment in CurrentDocument.AllAttachments) {
"<a href='/getattachment/" + attachment.AttachmentGUID + "'>" + attachment.AttachmentName + "</a> (" + 
Round(attachment.AttachmentSize/1024,0) + "KB)<hr />";
} #%}
1 votesVote for this answer Mark as a Correct answer

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