+1-888-365-2779
Try Now
More in this section

Forums / Developing with Sitefinity / How to display file size of Image Library item

How to display file size of Image Library item

5 posts, 0 answered
  1. Nayan Suchak
    Nayan Suchak avatar
    3 posts
    Registered:
    12 Apr 2010
    20 Sep 2010
    Link to this post
    Hi,

    I have a user control with a property that contains a dynamic link to an Image. I'm using the following selector for picking an image:

    [WebEditor("Telerik.Libraries.WebControls.ImageSelector, Telerik.Libraries")]

    How do I get of of the file size of the selected image to display in a link? Ideally I would like to display the file size in MB or KB.
     
  2. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    25 Nov 2016
    20 Sep 2010
    Link to this post
    Hi Nayan Suchak,

    The selector you use returns the library item in the following format - ["ProviderName"]Guid.

    After you return the library item you can easily get IContent object and from there the item size.

    sample

    protected void Page_Load(object sender, EventArgs e)
    {
        var cntitem = GetContentItem(this.Image);
        byte[] buffer = (byte[])cntitem.Content;
    }
     
     
    public IContent GetContentItem(string value)
    {
            int idx = value.IndexOf("]");
            string provider = value.Substring(1, idx - 1);
            string strId = value.Substring(idx + 1);
            Guid id = new Guid(strId);
            var manager = new ContentManager(provider);
     
            return manager.GetContent(new Guid(strId));
    }


    Regards,
    Ivan Dimitrov
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  3. Nayan Suchak
    Nayan Suchak avatar
    3 posts
    Registered:
    12 Apr 2010
    20 Sep 2010
    Link to this post
    Thanks Ivan,

    Once I obtain a reference to the Content Item, how do I actually get the size? Do I need to read the Meta Field? Are there any helper methods/classes to display the size in a more friendly manner (Kb and Mb instead of Byes)?
  4. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    25 Nov 2016
    20 Sep 2010
    Link to this post
    Hello Nayan Suchak,

    Yes you can read the Size metakey by using GetMetadata("Size") and cast the value to integer. Then divide the value to 1048576 (1 Megabyte = 1048576 Bytes)

    Best wishes,
    Ivan Dimitrov
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  5. Nayan Suchak
    Nayan Suchak avatar
    3 posts
    Registered:
    12 Apr 2010
    20 Sep 2010
    Link to this post
    Thank you veru much for your help and prompt resonses.
Register for webinar
5 posts, 0 answered