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'文件上传成功')

版权声明:
作者:Joe.Ye
链接:https://www.appblog.cn/index.php/2023/02/25/retrofit2-learning-8-server-upload-file-design/
来源:APP全栈技术分享
文章版权归作者所有,未经允许请勿转载。

THE END
分享
二维码
打赏
海报
Retrofit2学习之八:服务端上传文件设计
Retrofit2学习服务端设计在[OKHttp3学习服务端设计](http://www.appblog.cn/2016/11/18/Python搭建HTTP服务器:API接口(支持RESTful API)/)的基础上增加上传……
<<上一篇
下一篇>>
文章目录
关闭
目 录