如何使用flask_restful上传多个文件? [英] how to upload multiple files with flask_restful?

查看:282
本文介绍了如何使用flask_restful上传多个文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用flask_restful上传多个文件,但是除了第一个文件名之外,无法在参数中获取文件名列表,如何获取带有args的文件列表?

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?

这是我的代码,

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
        ......

我得到的是:

 * 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 -

实际上我选择了两个文件simple_api-master.zip,simple_api-master-old.rar并通过post方法传递值,因此打印功能应输出u'simple_api-master.zip',u'simple_api-master- old.rar",但现在它仅输出第一个文件名,我该怎么做才能获取文件列表?

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?

推荐答案

基本上,只需添加

action ='append'

action='append'

您的代码现在应该为:parser.add_argument('file', type=werkzeug.FileStorage, location='files',required=True, action='append')

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

这篇关于如何使用flask_restful上传多个文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆