Retrofit2学习之八:服务端上传文件设计

Retrofit2学习服务端设计在[OKHttp3学习服务端设计](http://www.appblog.cn/2016/11/18/Python搭建HTTP服务器:API接口(支持RESTful API)/)的基础上增加上传多个文件功能,仍然使用 Python Flask框架实现。

多个域上传多个文件

# 文件上传API - 多个域上传多个文件
@app.route("/upload/multiple", methods=['POST'])
def upload_multiple():
    description = request.form.get("description")
    print 'description = ' + description

    file1 = request.files['file1']
    file2 = request.files['file2']

    if file1 and allowed_file(file1.filename):
        filename = secure_filename(file1.filename)
        print 'file1 = ' + filename
        file1.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))

    if file2 and allowed_file(file2.filename):
        filename = secure_filename(file2.filename)
        print 'file2 = ' + filename
        file2.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))

    return jsonify(code=1, message=u'文件上传成功')

同一个域上传多个文件

# 文件上传API,同一个域上传多个文件
@app.route("/upload/multiples", methods=['POST'])
def upload_multiples():
    description = request.form.get("description")
    print 'description = ' + description

    #files为关键字,不可作为变量
    uploaded_files = request.files.getlist("file")
    for file in uploaded_files:
        if file and allowed_file(file.filename):
            filename = secure_filename(file.filename)
            print 'file = ' + filename
            file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))

    return jsonify(code=1, message=u'文件上传成功')
上一篇 Retrofit2学习之七:上传多个文件
下一篇 Retrofit2学习之九:与RxJava结合
目录
文章列表
1 CentOS 7下安装RabbitMQ
CentOS 7下安装RabbitMQ
2
Retrofit2学习之一:HelloWorld
Retrofit2学习之一:HelloWorld
3
AWS SFTP Transfer 下载通过S3上传的文件(非SFTP上传)权限问题
AWS SFTP Transfer 下载通过S3上传的文件(非SFTP上传)权限问题
4
SpringBoot+Lucene第四篇 — 入门代码
SpringBoot+Lucene第四篇 — 入门代码
5
MySQL 与 Redis 缓存的同步方案
MySQL 与 Redis 缓存的同步方案
最新评论
一位WordPress评论者
一位WordPress评论者
2月12日
您好,这是一条评论。若需要审核、编辑或删除评论,请访问仪表盘的评论界面。评论者头像来自 Gravatar。