bg

Python FLask 保存上传的文件


Posted on January 18, 2021
file.py

_19
@app.route('/api/upload/', methods=['POST'])
_19
def upload():
_19
# 获取文件
_19
uploaded_file = request.files['file']
_19
# 文件后缀(文件全名-> request.files['file'].filename)
_19
file_extension = uploaded_file.filename.split('.')[-1]
_19
# MIME 类型
_19
mimetype = uploaded_file.mimetype
_19
# 保存到 当前文件夹+'files/'
_19
save_target = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'files/', uploaded_file.filename)
_19
# 创建/files
_19
if not os.path.isdir(os.path.dirname(os.path.abspath(__file__)), 'files/'):
_19
os.mkdir(os.path.dirname(os.path.abspath(__file__)), 'files/')
_19
# 保存
_19
uploaded_file.save(save_target)
_19
# return看自己需求填写
_19
return jsonify(
_19
code=200
_19
)

© cocdeshijie. All rights reserved.