Python Flask:返回的文件不可读

问题描述 投票:-1回答:1

[在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不允许没有响应对象的文件返回,并且响应对象不可读并且不支持直接保存该文件。

python flask response flask-restful sendfile
1个回答
0
投票

C0]中的Flask 服务器代码正确。一个更完整的示例:

Case 1

但是@app.route('/download') def download_file(): # Some logic here send_file('any_file.pdf') 返回的Response对象没有Response方法。正确的方法是使用:

requests.get:响应的内容,以字节为单位。

因此,client代码应为:

read
© www.soinside.com 2019 - 2024. All rights reserved.