6

I'm trying upload multiple files with flask_restful, but can't get the files name list in the arguments except the first file name, how can i get the files list with args?

here is my code,

from models import Server
import werkzeug
from  werkzeug import secure_filename
from settings import upload_folder,allowed_extensions,currentWorkingPath,os,sys,reqparse,Resource
from settings import fields,marshal_with,abort
from settings import redirect, url_for


'''
#######################################################Uploads API
'''
uploads_fields = {
    'uri': fields.Url('uploads', absolute=True)
}


parser = reqparse.RequestParser()
parser.add_argument('file', type=werkzeug.FileStorage, location='files',required=True)

class Uploads(Resource):
    @marshal_with(uploads_fields)
    def post(self):
        args = parser.parse_args()

        print 'file',args
        ......

what i got is:

 * Restarting with stat
 * Debugger is active!
 * Debugger pin code: 115-504-357
 * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
file {'file': <FileStorage: u'simple_api-master.zip' ('application/octet-stream'
)>}
127.0.0.1 - - [18/Jan/2017 10:40:38] "POST /uploads/ HTTP/1.1" 200 -

in fact I selected two files simple_api-master.zip,simple_api-master-old.rar and passed value throught post method, so the print function should output u'simple_api-master.zip',u'simple_api-master-old.rar', but now it only output the first file name, what should I do to get the files list?

1 Answer 1

10

Basically, just add

action='append'

Your code should be now: parser.add_argument('file', type=werkzeug.FileStorage, location='files',required=True, action='append')

Sign up to request clarification or add additional context in comments.

2 Comments

It results in error though, for some reason. 413: "The data value transmitted exceeds the capacity limit". If I upload the same file without action='append' it works fine.
Okay, update here, the issue was due to Swagger sending request as x-www-form-urlencoded while werkzeug expects form-data.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.