>From my research, it looks like maybe an onvalidation function is the way
to do this. Can someone confirm this?
Something like:
def validate_upload():
passed_validation = []
files = request.vars['upload_files']
for file in files:
extension_file = file.split('.')[-1]
if extension_file == [csv|txt]
passed_validation.append(file)
return passed_validation
Thoughts?
On Thursday, January 9, 2014 2:57:40 PM UTC-8, Brando wrote:
>
> Thanks to Calvin Morrison's excellent work here is a really simple
> solution to uploading multiple items. I need help validating multiple
> files. I can upload one .txt file without issue; however, if i choose more
> than one it will not pass validation. Can someone tell me what kind of
> loop or function would help me validate files BEFORE they are uploaded?
> Also, if there is a more elegant way to code the controller please let me
> know.
>
> Model:
> db.define_table('uploads',
> Field('username', 'string'),
> Field('filename', represent = lambda x, row: "None" if x == None else
> x[:45]),
> Field('up_file', 'upload', uploadseparate=True,
> requires=IS_NOT_EMPTY()),
> Field('up_date', 'datetime'),
> Field('up_size', 'integer', represent= lambda x, row:
> quikr_utils.sizeof_fmt(x) ),
> Field('notes', 'text'))
>
> Controller:
> def submit():
> import datetime
> form = FORM(LABEL("File(s):"), INPUT(_name='up_files', _type='file',
> _multiple=True,
> requires=IS_UPLOAD_FILENAME(extension='txt|config|log')),INPUT(_type='submit'))
> if form.accepts(request.vars, formname="form"):
> response.flash = 'form accepted'
> files = request.vars['up_files']
> if not isinstance(files, list):
> files = [files]
> for f in files:
> up_file = db.uploads.up_file.store(f, f.filename)
> i = db.uploads.insert(notes=request.vars.notes,
> up_file=up_file, filename=f.filename, up_date= datetime.datetime.now())
> db.commit()
> elif form.errors:
> response.flash = 'form has errors'
> else:
> response.flash = 'please fill the form'
> return dict(form=form)
>
>
>
--
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/groups/opt_out.