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
Hope this helps:
if (media != null)
using (var stream = media.BinaryData.OpenRead())
long sizeInBites = stream.Length;
Thanks Dejan that works fine!
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.
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.
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.