Multiple File Upload

90 views
Skip to first unread message

Jason Macgowan

unread,
Oct 18, 2012, 7:22:31 PM10/18/12
to we...@googlegroups.com
I am using Dojo to upload multiple files to the server at once.  It sends a POST request to my Web.py app, submitting the files under the attribute name 'uploadedfiles'.

I try to catch and return these files with the following code:

def POST(self):
    x = web.input()
    return x.uploadedfiles

Works great with one file, but with 2 or more it returns only the last file.

I Read the Docs; says web.input uses a dict-like object, so the behavior above makes sense.

So reading up on things, I try:
def POST(self):
    x = web.input(uploadedfiles=[])
    return x.uploadedfiles

Cool!  Now it returns a list with both files!  But it's just the file itself.  It's not a storage object like it would be if there was just one file.

My question is:

Is there a way to return a list of storage objects from web.input()

Andrey Kuzmin

unread,
Oct 19, 2012, 3:13:29 AM10/19/12
to we...@googlegroups.com
x is a storage object that web.input() returns. file or list of files is value of x.uploadedfiles. storage object is not for storing files, its basically a dict what values you can get via dot syntax.

Jason Macgowan

unread,
Oct 19, 2012, 8:37:26 AM10/19/12
to we...@googlegroups.com
Right, but the issue is that my uploaded file list is just a list with
strings. I need a way to access both of the uploaded files
> --
> You received this message because you are subscribed to the Google Groups
> "web.py" group.
> To view this discussion on the web visit
> https://groups.google.com/d/msg/webpy/-/8S7bccaJ9ZsJ.
>
> To post to this group, send email to we...@googlegroups.com.
> To unsubscribe from this group, send email to
> webpy+un...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/webpy?hl=en.

Jason Macgowan

unread,
Oct 19, 2012, 10:00:27 AM10/19/12
to we...@googlegroups.com
Just to make sure I'm asking what I'm trying to ask, consider the following HTML

<form method="POST" enctype="multipart/form-data">
<input type="file" name="uploadedfiles" />
<input type="file" name="uploadedfiles" />
<input type="submit" />
</form

This is bad markup, yes, but it's only to demonstrate the content of
the POST request that Dojo's mutli uploader creates.

My question is, how do I access the files from this POST request?

web.input(uploadedfile={}) will only give me the last file
web.input(uploadedfile=[]) will only give me a list of strings from the files

I'm stumped, so any help is appreciated.

Andrey Kuzmin

unread,
Oct 19, 2012, 10:04:44 AM10/19/12
to we...@googlegroups.com
This is what I do to process multiple files:

        i = web.webapi.rawinput()
        files = i.uploadedfiles
        if not isinstance(files, list):
            files = [files]
        for f in files:
            # f.filename, f.file

Jason Macgowan

unread,
Oct 19, 2012, 11:05:25 AM10/19/12
to we...@googlegroups.com
Works like a dream, as usual Andrey!
> https://groups.google.com/d/msg/webpy/-/LxlMnUUNTi8J.
Reply all
Reply to author
Forward
0 new messages