I am uploading multiple images through file inputs where the name
and number of inputs is dynamic.
They do follow this naming convention however:
<input name = "image_path" ...
<input name = "image_path_F1" ...
<input name = "image_path_F2" ...
<input name = "image_path_F3" ...
The inputs are sent as FormData objects.
When handling a single image from the Python scripts I have previously used:
uploaded_image = request.files.name_of_file_input_here
Question
Is there a generic 'catch all' type of method available with request.files
which could be used like:
uploaded_images = request.files.*all
Or will I need to create some sort of loop to handle the possible file names eg:
Client Side (jQuery)
var names_array = ["image_path","image_path_F1","image_path_F2"];
var length_of_names_array = 3;
sent to Python...
Python
names_array = request.forms.names_array
length_of_names_array = request.forms.length_of_names_array
counter = 1
for i, val in enumerate(range(length_of_names_array)):
if i == 0:
constructor = "image_path"
request.files.constructor
else:
constructor = "image_path_F" + str(counter)
request.files.constructor
counter += 1
The above code will just generate the correct names (and actually I'm not sure if the request.files
method above would work with constructor
- edit, it doesn't seem to).
There may be a solution to be found in the approach here:
https://stackoverflow.com/a/3111795/1063287
But I don't quite understand how it operates or exactly how it could be applied to the above scenario:
If you don't know the key, you can iterate over the files:
for filename, file in request.FILES.iteritems():
name = request.FILES[filename].name