Gino
Gino

Reputation: 951

uploading multiple files with pyramid

Trying to upload multiple files at once using python. The upload.html source code is as followed:

        <form name="frmRegister" method="post" accept-charset="utf-8" enctype="multipart/form-data" class="form-horizontal">
             <div class="control-group">
                 <div class="controls">
                    <input type="file" name="files" multiple='multiple'>
                 </div>
             </div>
             <div class="control-group">
                 <div class="controls">
                    <input class="btn btn-primary" type="submit" name="btnSubmit" value="Add Product" />
                 </div>
             </div>
        </form>

in my admin.py:

    @view_config(context="mycart:resources.Product", name="add", renderer='admin/mall/product/add.jinja2', permission = 'admin')
    @view_config(context="mycart:resources.Product", name="add", request_method="POST",  renderer='admin/mall/product/add.jinja2', permission = 'admin')
    def product_add(context, request):
        if 'btnSubmit' in request.POST:
            print ("files >>> ", request.POST['files'])

in my terminal, it is showing just FieldStorage('files', u'DSC01973.JPG') whereas I've selected 'DSC01975.JPG', 'DSC01976.JPG'.

Why is this so?

Upvotes: 6

Views: 2534

Answers (2)

I could solve the problem with the following function:

from cgi import FieldStorage
def get_all_file_data_list(request):
    return [x for x in request.POST.values() if isinstance(x, FieldStorage)]

Upvotes: 1

Gino
Gino

Reputation: 951

I've found a way to solve it, I believe there are many others, if there are, please feel free to holler out:

    fileslist = request.POST.getall('files')
    print ("My files listing: ", fileslist)
    for f in fileslist:
        print ( "individual files: ", f )

Upvotes: 10

Related Questions