- 接口返回真实的文件
@mod.route("/download/", methods=['GET'])
def download_file():
file_path = request.args.get("filePath", "E:\\photo\\git.png")
print(file_path)
try:
if os.path.isdir(file_path):
return jsonify({"ret": -1, "msg": "Can not download"})
else:
name = file_path.split('\\')[-1] # 切割出文件名称
print(name)
file_dic_path = file_path.replace(name, '')
print(file_dic_path)
return send_from_directory(directory=file_dic_path, path=name, as_attachment=True)
except Exception as e:
print(e)
return jsonify({"ret": -1, "msg": "The file is not exist or can not download"})
主要是切割出目录和文件名来。
- send_from_directory
注意这个方法,参数有变化。
The changelog at the bottom that says "Changed in version 2.0: path replaces the filename parameter."
https://stackoverflow.com/questions/67591467/flask-shows-typeerror-send-from-directory-missing-1-required-positional-argum