[在python flask中实现rest API时,我使用了几个选项来返回文件(任何类型),读取并将其保存到请求的本地存储库,但遇到多个错误,如下所示:
案例1:
def download_file():
return send_file('any_file.pdf')
r = requests.get(url = 'http://localhost:5000/download').read()
已返回错误响应对象没有属性读取/文本/内容
案例2:
def download_file():
file = open('any_file.pdf','r').read()
return file
r = requests.get(url = 'http://localhost:5000/download')
已返回错误返回不接受此
所以我该怎么做,因为flask不允许没有响应对象的文件返回,并且响应对象不可读并且不支持直接保存该文件。
C0]中的Flask 服务器代码正确。一个更完整的示例:
Case 1
但是@app.route('/download')
def download_file():
# Some logic here
send_file('any_file.pdf')
返回的Response
对象没有Response
方法。正确的方法是使用:
requests.get
:响应的内容,以字节为单位。
因此,client代码应为:
read