Try our conversational search powered by Generative AI!

How to retrieve the file size of a file in VPP

Vote:
 

Hi,

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:-

#204734
Jun 14, 2019 9:39
Vote:
 

Hi Losi,

What is the error message you are seeing? 

#204737
Jun 14, 2019 14:29
Vote:
 

Hi Losi,

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.

#204738
Jun 14, 2019 15:12
Vote:
 

Hi Losi,

Please refer the below article to get the mediadata size - 

https://episerverblog.wordpress.com/2014/06/11/displaying-file-size-for-your-mediadata/

Thanks

Ravindra

#204759
Jun 17, 2019 9:23
* 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.