Donnerstag, 17. September 2009

Handling multiple file uploads in Turbogears2

Just as a reminder to myself, here's the trick needed in order to handle multiple file uploads using the same name. What does "using the same name" mean? Well, if you use javascript to make your web app's user happy - take http://www.fyneworks.com/jquery/multiple-file-upload/ for example, then you would write something like this in your html:

<form action="u" enctype="multipart/form-data" method="POST">
<input class="multi" maxlength="2" name="doc" type="file" />
<input type="submit" value="Upload" />
</form>

Using trickery like the jquery.MultiFile (from the link above) - i.e. sending multiple files using the same input field, what will happen is that all the files will arrive through request.POST['doc'] - i.e. you don't automatically get a list of files such as request.POST['doc'] = [FileStorage(), FileStorage()] - which would probably be exactly what you want in order to iterate through. Instead, you have to use the request.POST.getall('doc') to generate a list object containing all of the uploaded files.