Calculate the size of uploaded file using compute and lambda function

If you need to display the size of uploaded file (any file: jpg, doc, w2p...) you can use compute function to save file size information in to the database. This method is used on web2pyref.

Model db.py


Standard table definition.















from os import path


db.define_table('file',
Field('name', 'string', label=T('File Name')),
Field('website_url','string', length="512", label=T('Source Link')),
Field('table_name',readable=False, writable=False),
Field('record_id','integer',readable=False, writable=False),
Field('title', label=T('Title'), notnull=True),
Field('comment', label=T('Comment')),
Field('version','string', length="32", label=T('Version Number')),
Field('file', 'upload', autodelete=True, label=T('File')),
Field('size', 'double', readable=False, writable=False, label=T('Size')),
format='%(title)s'
)
db.file.size.compute = lambda row: path.getsize(path.join(request.folder,'uploads',row.file))

Controller default.py


Below code will query all records in db.file table.



def show_files():

files= db(db.file>0).select()
return dict(files=files)

View default/show_file.html


Create new file named default/show_files.html and open following page in your browser: http:127.0.0.1/appname/default/show_files.html (relpace appneame)
















 {{for record in files:}}

<div class="visible_result">
<a rel="nofollow" href="{{=URL('references','download', args=record.file)}}" target="_blank" class="list-group-item" target="_blank">
<h4 class="list-group-item-heading">{{=record.name}}<small> Version: {{=record.version}}</small></h4>
<p class="list-group-item-text">
{{if record.size:}}
{{if record.size<1000000:}}
{{=record.size/1000 }} kB
{{else:}}
{{=record.size/1000/1000 }} mB
{{pass}}- {{=record.comment}}
{{pass}}
</p>
</a>
</div>
{{pass}}

Result

compute

loading...

Comments

loading...

Tag Cloud

accepted Acces Control access rights actions_disabled admin allowed_attributes allowed_schemes App apps args auth authentication B bar chart BEAUTIFY begin BODY BR carousel CAT CENTER chart check CLEANUP client request client_side CMS CODE COL COLGROUP comment compute contact form crud.create crud.delete crud.read crud.select crud.tables CRUD custom attributes custom form DAL data define_table deletable depriciated DIV doctype dot dspval EM embed64 enable_record_version end error_message example extension Field types Field FIELDSET file filename form.accepted form.accepts form.process form.validate FORM format Forms Grid H1 H2 H3 H4 H5 H6 hash_vars Hello helper helpers highchart hmac_key host hosting HTML image images inpval ip address ip IS_DATETIME IS_DATETIME_IN_RANGE IS_DECIMAL_IN_RANGE IS_EMPTY_OR IS_EQUAL_TO IS_EXPR IS_FLOAT_IN_RANGE IS_IMAGE IS_INT_IN_RANGE IS_IN_DB IS_IN_SET IS_IPV4 IS_LIST_OF IS_NOT_EMPTY IS_NOT_IN_DB IS_NULL_OR is_slug IS_STRONG IS_TIME IS_UPLOAD_FILENAME IS_UPPER IS_URL lable lambda lang lastdot linkto li_class li_first li_last login_methods maximum maxip maxlen MENU min minimum minip mobile mode module multiple permitted_tags pie chart placeholder plugin prepend_scheme pythonanywhere query rating reCaptcha redirect request.now routes.py salt sanitize scheme services settings slug sortable special SQLFORM() SQLFORM.grid SQLFORM SSL submit table constructor TAG test text TinyWebsite ul_class update upper URL rewrite URL validator value vars Views widget XML xmlescape _class _common_fields


Powered by
Web2py

Hosted on
www.pythonanywhere.com
(affiliated link)