I've uploaded a document to the VPP, and I've since linked to it in a page. Im trying to display the file extension and file size next to the link. I've been searching thorugh the forums and the closest I've come is the following:-
filepath = "\globalassets\documents\gt8amniocentesis0111.pdf"
var file = HostingEnvironment.VirtualPathProvider.GetFile(filepath) as UnifiedFile;
return file.Length / 1024 + "Kb"
Except each time I try to use this I get the error:-
What is the error message you are seeing?
Out of interest, is this an older version of Episerver (<7.5)? The references to VPP and UnifiedFile would imply an older version of the CMS.
If you're on a newer version, media data is available through the IContentLoader and IContentRepository. You can load your media item and access the file data via the BinaryData property so you can get the size like this:
private double GetFileSize(MediaData mediaContent)
using (var stream = mediaContent.BinaryData.OpenRead())
return stream.Length / (1024f * 1024f);
For the sake of efficiency, in scenarios where you're showing the file size on the front-end of your site I'd recommend creating a property on your media data to store the size then setting it once when the file is uploaded or updated (using an initializablemodule) rather than calculating the size every time it's requested.
Please refer the below article to get the mediadata size -