Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Uploading multiple files in a single request using python requests module

The Python requests module provides good documentation on how to upload a single file in a single request:

 files = {'file': open('report.xls', 'rb')} 

I tried extending that example by using this code in an attempt to upload multiple files:

 files = {'file': [open('report.xls', 'rb'), open('report2.xls, 'rb')]} 

but it resulted in this error:

 File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib.py",      line 1052, in splittype  match = _typeprog.match(url)  TypeError: expected string or buffer 

Is it possible to upload a list of files in a single request using this module, and how?

like image 598
user462455 Avatar asked Aug 12 '13 03:08

user462455


People also ask

Does Python Django support multiple file upload?

Here is a quick example of how to add a multiple file form to your Django application. Most multiple-upload front-ends were created without Django in mind, so interfacing with tools is not always straightforward as it might be with a different language.


1 Answers

To upload a list of files with the same key value in a single request, you can create a list of tuples with the first item in each tuple as the key value and the file object as the second:

files = [('file', open('report.xls', 'rb')), ('file', open('report2.xls', 'rb'))] 
like image 133
Ryan Fau Avatar answered Sep 22 '22 17:09

Ryan Fau