In one of my application I store uploaded images in separte folders
in table definitionl:
Field('imageFilename', length=128, writable=False, readable=False),
Field('image', type='upload', requires=[IS_IMAGE(extensions=('gif', 'jpg',
'jpeg', 'png'), maxsize=(240, 100), error_message='maxWidth is 240 pixels
maxHeight is 100 pixels. File format: gif, jpg, jpeg, png'),
IS_LENGTH(48*1024, error_message='File size exceeds 24 KB')],
autodelete=True),
in function:
folder = 'user' + str(auth_user.id)
db[table].image.uploadfolder =
os.path.join(request.env.applications_parent, 'applications',
'dbmodel', 'static',
'uploads', folder)
form = SQLFORM(table, record, deletable=True, showid=False,
upload=URL('dbmodel', 'static/uploads', folder,
args=request.vars.image))
if hasattr(request.vars.image, 'filename'):
form.vars.imageFilename = request.vars.image.filename
In case the image field can be empty I use requires =
IS_EMPTY_OR(IS_IMAGE(), IS_LENGTH())
and in the function:
if form.process().accepted:
if record and form.vars.image__delete:
file = record.image
os.remove(os.path.join(request.env.applications_parent,'applications',
'dbmodel', 'static', 'uploads', folder,
file))
record.update_record(image=None, imageFileName=None)
to delete the image from the folder.
Annet
--
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
---
You received this message because you are subscribed to the Google Groups
"web2py-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/d/optout.