Success!  Here is how you can validate all files in a multiple file upload. 
 In this example if one document does not validate then the entire 
validation fails.  You could easily just drop the files that don't validate 
and only let the accepted files through for processing.  

*If anyone has a more elegant solution please let me know.*

def submit():
    form = FORM(LABEL(""), INPUT(_name='up_files', _type='file', 
_multiple=True, requires=IS_NOT_EMPTY()),INPUT(_type='submit')) # The 
multiple param lets us choose multiple files.
    if form.accepts(request.vars, formname="file_upload", 
onvalidation=upload_validation): #onvalidation checks the uploaded files to 
make sure they are only txt, config, or log.
        response.flash = 'Bro...uploading files' 
        files = request.vars['up_files'] 
        if not isinstance(files, list): #convert files to a list if they 
are not one already.
            files = [files]
        for file in files:
            up_file = db.uploads.up_file.store(file, file.filename) #store 
is a FIELD method that let's you save a file to disk.  you can choose the 
directory if you want using the 'path' param.
    else:
        response.flash = 'Choose the Files you would like to upload'
    return dict(form=form)


def upload_validation(form):
    files = request.vars['up_files']
    if not isinstance(files, list):
        files = [files]
    for file in files:
        file_extension = file.filename.split('.')[-1] #find the file 
extension
        acceptable_file = file_extension in ('txt', 'config', 'log') 
#choose the allowed file extensions
        if not acceptable_file:
            form.errors.up_files = "Only .txt, .config, or .log files are 
allowed." #if they files are not acceptable return False. Validation 
fails....else process the files.
            return False



-- 
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 web2py+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Reply via email to