File extension type and size

Conor Dunk asked on August 24, 2016 14:33

Hi,

I have seen a few posts regarding this but non seem to help, i'm trying to pull a few details from the File API.

  1. File type I.E. pdf, doc, jpg
  2. The file size.

Iv'e added a method in my CMSTransformation class. It's can't find the AttachmentManager, CurrentDocument, ValidationHelper extension methods.

Iv'e added the code for attachment size below:

public static string GetAttachmentSize(object fileAttachmentGuidId)
    {
        // Get current attachment GUID
        Guid attachGuid = CMS.GlobalHelper.ValidationHelper.GetGuid(fileAttachmentGuidId, Guid.Empty);
        if (attachGuid != Guid.Empty)
        {
            // Get the attachment
            AttachmentManager am = new AttachmentManager();
            AttachmentInfo ai = am.GetAttachmentInfo(attachGuid, CMSContext.CurrentSite.SiteName);
            if (ai != null)
            {
                string attExtension = ai.AttachmentExtension;
                return DataHelper.FileSizeFormat(ai.AttachmentSize);
            }
        }
        return string.Empty;
    }

I can't find any examples for the file type, has anyone done this before?

Thank you, Conor

Recent Answers


Brenden Kehren answered on August 24, 2016 14:39

The code you posted is from v7 and your tag says you're working in v9. You'll need to update your code.

CMS.GlobalHelper is now CMS.Helpers

CMS.DocumentEngine.AttachmentManager is now CMS.DocumentEngine.AttachmentInfoProvider

CMSContext.CurrentSite.SiteName is now SiteContext.CurrentSiteName

0 votesVote for this answer Mark as a Correct answer

Richard Sustek answered on August 24, 2016 14:57 (last edited on August 24, 2016 14:57)

public static string GetAttachmentSize(object fileAttachmentGuidId)
{
    var attachmentGuid = CMS.Helpers.ValidationHelper.GetGuid(fileAttachmentGuidId, Guid.Empty);
    if (attachmentGuid != Guid.Empty)
    {
        // Get the attachment
        var ai = CMS.DocumentEngine.AttachmentInfoProvider.GetAttachmentInfo(attachmentGuid, SiteContext.CurrentSiteName);
        if (ai != null)
        {
            string attExtension = ai.AttachmentExtension;
            return CMS.Helpers.DataHelper.FileSizeFormat(ai.AttachmentSize);
        }
    }
    return string.Empty;
}

If you use this within transformation (which you most probably will since its transformation method:)) then be aware that this will cause 1 SQL query per each item in your transformation. I would highly recommend to cache the result of transformation or create custom SQL query in order to join your data with attachment directly (if possible).

0 votesVote for this answer Mark as a Correct answer

Conor Dunk answered on August 25, 2016 12:50

Thank you, what do I need to pass into the method, i'm currently doing this:

<%# GetAttachmentSize(EvalInteger("AttachmentID")) %>

But that just returns 0?

Thank you, Conor

0 votesVote for this answer Mark as a Correct answer

Matthew Sandstrom answered on August 26, 2016 13:24

Hi Conor, The method accepts a guid (global unique identifier) as the parameter, not the attachment id. It is a property of the AttachmentInfo class

However, It looks like you're using a transformation, so I'm wondering if you're using a attachments datasource? In this case, you don't even need the custom method. You can simply get these values directly from the datasource, as they are stored in the CMS_Attachment table directly.

For example:

<div>
<h3><%# Eval("AttachmentName")%></h3>
<p>Extension: <%# Eval("AttachmentExtension")%></p>
<p>Size: <%#Eval("AttachmentSize")%> Bytes</p>
</div>

Just make sure that these columns are included if you're filtering columns in the datasource.

0 votesVote for this answer Mark as a Correct answer

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