Try our conversational search powered by Generative AI!

How to get media File size


I use the following code to retrieve my media file, but do not know how to get the file size of the requested file

var contentRepository = ServiceLocator.Current.GetInstance<IContentRepository>();
var contentLink = new ContentReference(57);
var media = contentRepository.Get<MediaFile>(contentLink);

Can anyone help

Feb 11, 2014 15:54

Hope this helps:

if (media != null)
    using (var stream = media.BinaryData.OpenRead())
        long sizeInBites = stream.Length;


Feb 11, 2014 16:31

Thanks Dejan that works fine!

Feb 12, 2014 9:52

Hi, Is this the official way to go, to obtain the file size, seems like an overhead creating a stream just for the size.

I am a bit worried about the performance impact when dealing with larger amounts of files.


Jun 10, 2014 9:00

Hey, Daniel, 

You might consider a property Size (with ScaffoldColumn(false)) on the ImageFile that is filled once when image is created. That way, you just read the size, but you set it only once.

Jun 10, 2014 9:25

Hi Marija thank you for your suggestion, I was thinking about doing as you suggested but I am a bit lazy and expected this to be a built in feature in MediaData class.


Jun 10, 2014 9:34
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.