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

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

多个域上传多个文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# 文件上传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'文件上传成功')

同一个域上传多个文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# 文件上传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'文件上传成功')

Powered by AppBlog.CN     浙ICP备14037229号

Copyright © 2012 - 2020 APP开发技术博客 All Rights Reserved.

访客数 : | 访问量 :