Paul Gwamanda
Paul Gwamanda

Reputation: 302

Get uploaded KB/MB value of uploaded file field in umbraco

Trying to get uploaded KB/MB value of uploaded field in umbraco.

The below code works fine if we were getting the file size of the item but i want the file size of the upload field @item.GetPropertyValue("uploadPDF"), how do i do that?

    @foreach (var item in selection){

       // Get uploaded file size in MB
       var bytes = Convert.ToInt64(@item.GetPropertyValue<string>("umbracoBytes"));
       var x = ((Math.Round(Convert.ToDecimal(@bytes) / 1048576, 1)).ToString() + " MB");
       var y = ((Math.Round(Convert.ToDecimal(@bytes) / 1024, 1)).ToString() + " KB");
       var fileSize = (bytes >= 1048576) ? x : y;

       // Loop through items and iterate 
       var pdf = @item.GetPropertyValue("uploadPDF");

           <div class="full-div">
               <a href="@pdf" target="_blank">
                  <img src="/images/pdf_icon.png" alt="">
                    @item.Name (@fileSize) 
                    // Get the file size of the pdf field instead of the item field
               </a>
           </div>
         }

Upvotes: 0

Views: 786

Answers (1)

Claus
Claus

Reputation: 1985

If you were using the built-in media types in Umbraco you should be able to get the size from the umbracoBytes property as long as you're using either File or Image media types.

It however sounds like your property is a Umbraco.UploadField existing directly on a document type, which means it does not have the file size available.

In your case, I would add a property to your type called: uploadPDFbytes, then create a IApplicationHandler and hook into the Saving event. Then when saving content that stores a PDF file - I would make sure that this handler checks the filesize of the file being saved and puts it in the uploadPDFbytes property (pretty similar to what Umbraco is doing to its own media uploads). Now you would be able to get the file size of the uploaded PDF by checking the uploadPDFbytes property.

Upvotes: 1

Related Questions