How to get media File size

Vote:
 

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

#81207
Feb 11, 2014 15:54
Vote:
 

Hope this helps:

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

    

#81208
Feb 11, 2014 16:31
Vote:
 

Thanks Dejan that works fine!

#81230
Feb 12, 2014 9:52
Vote:
 

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.

/Daniel

#87100
Jun 10, 2014 9:00
Vote:
 

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.

#87106
Jun 10, 2014 9:25
Vote:
 

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.

/D

#87108
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.