Coded something up in Couch in an interesting way? Have a snippet or shortcode to share? Post it here for the community to benefit.
7 posts Page 1 of 1
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?

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".
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.

Attachments

Hi KK

Just skim read the reply and it looks great! Thanks, il try it this eve, I will actually look into validator as they will be very useful for things like max image size etc.
Hi KK,

I can't seem to get this working, I've followed your instructions but when uploading a file it doesn't seem to put the file size in the new field.

Code: Select all
<?php require_once( 'couch/cms.php' ); ?>

<cms:template title='Downloads' name='downloads' clonable='1' commentable='0' dynamic_folders='1'>

<cms:editable name='download_description' label='Download Description' type='richtext' />

<cms:editable
  name='file_download'
  label='Downloadable File'
  desc='Upload the file here'
  type='file'
/>

<cms:editable
name='my_filesize'
type='text'
validator='FileSize::get'
assoc_field='file_download'
/>

<cms:editable name='external_link' label='External Link' type='text' desc='If this is a link rather than a download enter the full URL i.e. http://www.thesitename.com/pagename' />

</cms:template>

<cms:embed 'downloads.tpl' />

<?php COUCH::invoke(); ?>


My kfunctions in the addons folder looks like this:

<?php
if ( !defined('K_COUCH_DIR') ) die(); // cannot be loaded directly

//require_once( K_COUCH_DIR.'addons/cart/session.php' );
require_once( K_COUCH_DIR.'addons/cart/cart.php' ); // automatically includes session

// File Size check and output into a text field to display for downloads
require_once( K_COUCH_DIR.'addons/filesize.php' );


and I have the filesize.php in the addons folder.
Hi Patrick,

Sorry but the sample code I gave was missing a little something -
please amend the editable region definition to make it the follows
Code: Select all
<cms:editable
    name='my_filesize'
    type='text'
    validator='FileSize::get'
    assoc_field='file_download'
>0</cms:editable>

The problem with the original sample code was that it did not have any default value for the text region - and Couch simply (and understandably) skips validation routines for empty regions.

Hope this helps. Do let me know please.
Thanks.
Hi KK,

I can confirm this is now working perfectly! Thanks for your help on this one, I think it will be very useful for others.
Hi!
This plugin is very good for me and I want to use it.
But I have a fatal problem - after the first page refresh - values for file size becomes zero ...

Can anyone help?

Regards
7 posts Page 1 of 1

Who is online

In total there are 2 users online :: 0 registered, 0 hidden and 2 guests
(based on users active over the past 5 minutes)

Users browsing this forum: No registered users and 2 guests