I recently got into a situation where I have to return the size of selected pdf file. In Sitecore using Sitecore.Data.Items.MediaItem class, we can access file size, extension, mime type etc of selected media item.
I have written a function GetSitecoreMediaItemSizeInBytes(string itemId) which takes item id of media item and return the size of media item in bytes.
I have written a function GetSitecoreMediaItemSizeInBytes(string itemId) which takes item id of media item and return the size of media item in bytes.
public long GetSitecoreMediaItemSizeInBytes(string itemId) { long mediaItemSize = 0; if (Sitecore.Data.ID.IsID(itemId)) { Item sitecoreItem = Sitecore.Context.Database.GetItem(Sitecore.Data.ID.Parse(itemId)); if (sitecoreItem != null) { MediaItem mediaItem = new MediaItem(sitecoreItem); mediaItemSize = mediaItem.Size; } else { Sitecore.Diagnostics.Log.Error("Could not find given sitecore media item with id '" + itemId + "' ", this); } } return mediaItemSize; }Code explanation:
- Sitecore.Data.ID.IsID() : This method takes in a string guid and returns true if it's a valid guid in Sitecore . The string must be 38 characters long, start with a curly brace "{" and end with a curly brace "}".
For example :
string itemId = "{AD1495DB-36FC-4B7A-80B6-D6170AB7F3B1}"; - Sitecore.Data.ID.Parse() : This method takes in a string guid and returns a Sitecore ID object.
- Sitecore.Context.Database.GetItem() : This method takes in a Sitecore ID object and return a Sitecore Item object.
- MediaItem mediaItem = new MediaItem(sitecoreItem) : Using this piece of code, we are getting Sitecore Media Item object.
- mediaItem.Size : Returns size of media item in bytes.
- mediaItem.Extension : Returns file extension of media item. In my case; it was .pdf.
- mediaItem.MimeType : Returns Mime Type of media item. In my case; it was application/pdf.
instance.Response.Write(@"alert('"Message"');document.location.href='" url "'; ");
ReplyDeletei got that error
ReplyDeleteHello Abhinandan,
ReplyDeleteCan you please elaborate the error ?
Realy nice article, i was looking for this in many days without finding a good tutorial...!! Yhanks alot...
ReplyDeleteThanks Adnan. You can also subscribe for new blogs posts and like our facebook page to get new notifications. :)
ReplyDeleteGreat Can i get the same project code in VB.Net please? Thanks
ReplyDeleteYou can convert c# code into VB.NET by online utilities. Check http://converter.telerik.com/ or http://www.developerfusion.com/tools/convert/csharp-to-vb/
ReplyDeleteNice article thanks..
ReplyDeletehttp://www.aspdotnet-pools.com/2014/06/login-form-with-lightbbox-effect-in.html
Hi, Good article and neat code. I have implemented your code but i have 300 images in one gallery. so all the images are in one page. Can you please advice how to add pagination for the thumbnail images. Thanks
ReplyDeleteThanks a lot!
ReplyDelete