by
KK » Thu Apr 11, 2013 9:39 pm
Hi,
Is there a way in couch to show the size of a file on the front end, i.e. if I upload a PDF in the Couch admin area, can I <output> that download link and display the file size?
There is no built-in method to do so but we can surely wire-up one to get this functionality.
Or would I need to use the "message" field to put in some jquery that would detect the file size before upload and save it in a text field called "file_size".
Not sure how you plan to do it with JavaScript. I'd be very interested, though, in seeing this approach so do let us know if you implement it this way.
In the meanwhile, I implemented this using server-side programming. Here is how -
As you know, the editable regions support 'validators'. The built-in validators are documented.
What is less known is that we can also define our own custom validators.
This is what I've done to attach my custom code to an ordinary 'text' type editable region as its validator.
Attached is the code as a addon (filesize.php). Please place it in 'couch/addons' folder and include it by placing the following in
kfunctions.php file
- Code: Select all
require_once( K_COUCH_DIR.'addons/filesize.php' );
Now, assuming that the 'file' type region in your template is named 'my_file', add a new editable region of type 'text' like this:
<cms:editable
name='my_filesize'
type='text'
validator='FileSize::get'
assoc_field='my_file'
>0</cms:editable>
Please note that the 'validator' parameter of this new region is set to
FileSize::get and the 'assoc_field' is set to the name of your original 'file' type editable region.
Visit the template as super-admin for the changes to take effect. Now if you select a new file in the original region and save, the text region should automatically show the file's size.
To make things easier on the display side, I've also added a custom tag named 'size_readable'.
This is how we can use it to show the captured filesize in human-readable form
- Code: Select all
Size: <cms:size_readable my_filesize />
where 'my_filesize' is, of course, the name of the text editable region we added.
Hope this helps.
Please let me know.
Thanks.