Portal Engine Questions on portal engine and web parts.
Version 4.x > Portal Engine > Getting File Size.... View modes: 
User avatar
Member
Member
Jirr - 4/23/2009 3:05:19 PM
   
Getting File Size....
Hi,

How can I get the File size of a File in a File.Transformation code ?

Thanks

User avatar
Kentico Support
Kentico Support
kentico_radekm - 4/24/2009 10:56:54 AM
   
RE:Getting File Size....
Hello.

At first, you will need to call custom function in transformation (http://devnet.kentico.com/docs/devguide/adding_custom_functions_to_transformations.htm).
Then you will need to create appropriate object for attachment (http://devnet.kentico.com/docs/devguide/managing_attachments.htm) and get its size using appropriate propetry (AttachmentSize).

Best Regards,
Radek Macalik

User avatar
Member
Member
Jirr - 4/28/2009 1:28:40 PM
   
RE:Getting File Size....
Thanks

But another question and sorry for my ignorance, I've many files in the same place, so how can I pass the File Guid to my function to use it for return the file size ?

I know how to pass the fileID but that is useless for me...

Thanks again.

Jirr

User avatar
Member
Member
Jirr - 4/28/2009 2:01:20 PM
   
RE:Getting File Size....
I did it !!

Thanks and sorry to bother

=)

May God bless who is reading this.

User avatar
Kentico Support
Kentico Support
kentico_radekm - 4/29/2009 6:54:30 AM
   
RE:Getting File Size....
Hello,

just for others, here is the code which can be used:

public static  string  GetAttachmentFileSize(object  fileAttachmentGuidId)
{

// Get current attachment GUID

Guid attGuid = CMS.GlobalHelper.ValidationHelper.GetGuid(fileAttachmentGuidId, Guid.Empty);

if (attGuid != Guid.Empty)

{

// Get the attachment

AttachmentManager am = new AttachmentManager();

AttachmentInfo ai = am.GetAttachmentInfo(attGuid, CMSContext.CurrentSite.SiteName);

if (ai != null)

{

string attExtension = ai.AttachmentExtension;

return DataHelper.FileSizeFormat(ai.AttachmentSize);

}

}

return string.Empty;
}

Best Regards,
Radek Macalik

User avatar
Member
Member
Darren - 9/11/2009 8:57:45 AM
   
RE:Getting File Size....
Hi,

I've tried doing this but got:
The type or namespace name 'AttachmentManager' could not be found (are you missing a using directive or an assembly reference?)

..a soon as I loaded a page. What am I missing?

User avatar
Kentico Support
Kentico Support
kentico_radekm - 9/21/2009 11:11:32 AM
   
RE:Getting File Size....
Hello.

Could you please try to use using directive: using CMS.CMS.FileManager; or write it with full path, for example:

CMS.FileManager.AttachmentManager am = new CMS.FileManager.AttachmentManager(); ?

Thank you.

Best Regards,
Radek Macalik

User avatar
Member
Member
jkade - 6/4/2011 11:52:51 AM
   
RE:Getting File Size....
In case you need to reference this from an external assembly, make sure to include a reference to CMS.FileManager.dll.

User avatar
Member
Member
andreycoman-yahoo - 11/24/2011 3:57:31 AM
   
RE:Getting File Size....
Hi everyone,

I called this function in my transformation but I don't know what parameter to give or how to make that attachment object in the transformation.
I've tried with
<%# MyFunctions.GetAttachmentSize(EvalInteger("AttachmentID")) %>
but I don't think it needs that id.

How can I use this function in my transformation?

Best regards,
Andrei


User avatar
Kentico Support
Kentico Support
kentico_radekm - 12/6/2011 1:30:54 AM
   
RE:Getting File Size....
Hello.

If you pass AttachmentID as a parameter, you can get AttachmentInfo object in your custom macro’s code via CMS.TreeEngine .AttachmentManager.GetAttachmentInfo(AttachmentID,false) method.

This AttachmentInfo object contains AttachmentGUID property and when having this GUID, you can follow approach mentioned in this forum above.

Best Regards,
Radek Macalik

User avatar
Member
Member
kirannshetty-gmail - 2/20/2013 11:06:29 PM
   
RE:Getting File Size....
Hi Radek ,

How do we get filesize for files which are not stored in CMS_ATTACHMENT table ;

Thanks,
With Regards,
Kiran

User avatar
Kentico Support
Kentico Support
kentico_radekm - 3/4/2013 10:28:44 PM
   
RE:Getting File Size....
Hello.

Where exactly do you store those files, please?

Best Regards,
Radek Macalik

User avatar
Kentico Support
Kentico Support
kentico_radekm - 3/12/2013 2:24:30 AM
   
RE:Getting File Size....
Hello.

Let me share your solution with others:

We were able to get the file size by using below mentioned functions:
public static string GetAttachmentSize(object fileAttachmentGuidId)

{
// Get current attachment GUID
Guid attachGuid = CMS.GlobalHelper.ValidationHelper.GetGuid(fileAttachmentGuidId, Guid.Empty);

if (attachGuid != Guid.Empty && attachGuid != null)
{

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

public static string GetAttachmentSize(int NodeId)
{
// Get current attachment GUID
TreeProvider tree = new TreeProvider(CMSContext.CurrentUser);

if (tree != null)
{
TreeNode node = DocumentHelper.GetDocument(NodeId, CMSContext.PreferredCultureCode, tree);

if (node != null)
{
Guid guid = ValidationHelper.GetGuid(node.GetValue("FileAttachment"), Guid.Empty);

if (guid != Guid..Empty && guid != null)
{
AttachmentInfo atInfo = DocumentHelper.GetAttachment(node, guid, tree, false);

if (atInfo != null)
{
return DataHelper.FileSizeFormat(atInfo.AttachmentSize);
}
}
}
}

return string.Empty;

}

Thank you.

Best Regards,
Radek Macalik

User avatar
Member
Member
kirannshetty-gmail - 3/14/2013 12:15:51 PM
   
RE:Getting File Size....
Hi Radek

Sure,

Thanks,
With Regards,
Kiran