python - How to use restful upload multiple files with array? -
my schema:
test_multivalues = { 'name': {'type':'string'}, 'multi': {'type': 'list', 'schema': {'type': 'media'}}, 'arr': {'type': 'list'}, }
i use post data follow:
content-type: multipart/form-data
name: multivalue multi: ....file1... multi: ....file2.... arr: [arr_value1, arr_value2]
in eve,parameter arr
list, multi
first value. expect multi
list [file1, file2].
when read code, eve use werkzeug's multidict.to_dict()
in payload()
method return first value same key. how can key multiple values list ?
updated: eve raise exception above schema , post data: multi:must of list type
updated: yes, test curl.
curl -f "image=@text.txt" -f "image=@test.txt" http://localhost/eve/api
when changed code in payload() to:
v = lambda l: l if len(l) > 1 else l[0] return dict([(k, v(request.form.getlist(k))) k in request.form] + [(k, v(request.files.getlist(k))) k in request.files])
it return file list, eve's post method not support it, , throw exception.
ugly way solve this:
def saver(filestorageobj): app.media.put( filestorageobj, filename=filestorageobj.name, content_type=filestorageobj.mimetype, resource='test') def pre_test_post_callback(request): werkzeug.datastructures import immutablemultidict # files format: [("pics", filestorageobject)] pics = [saver( upfile[1] ) upfile in request.files.items(true) if upfile[0] == "pics"] form = request.form.copy() form['pics'] = pics request.form = immutablemultidict(form) request.files = immutablemultidict()
Comments
Post a Comment