Xha*_*ark 5 python rest httpresponse falconframework
我正在尝试使用 Falcon REST API 提供 pdf 文件,但我找不到任何示例。
到目前为止,这是我尝试过的:
class ReportResource:
def on_get(self, req, resp):
filename="./evaluation.pdf"
resp.downloadable_as = filename
resp.content_type = 'report/pdf'
resp.status = falcon.HTTP_200
Run Code Online (Sandbox Code Playgroud)
我得到的是一个空的pdf文件,当然我无法打开。我猜我没有在响应中加载文件内容,但我不是 python 专家,我不知道应该如何做。
如果我尝试这个:
resp.stream, resp.stream_len = open(filename)
Run Code Online (Sandbox Code Playgroud)
我收到此错误:
UnicodeDecodeError: 'charmap' codec can't decode byte 0x8d in position 130: character maps to <undefined>
Run Code Online (Sandbox Code Playgroud)
我完全确定该文件就在那里。
好的,完成了。我需要以二进制模式打开该文件。我把它留在这里,以防有人需要这个例子,因为我自己找不到人。
def on_get(self, req, resp):
filename="./evaluation.pdf"
resp.downloadable_as = filename
resp.content_type = 'application/pdf'
resp.stream= open(filename, 'rb')
resp.status = falcon.HTTP_200
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1039 次 |
| 最近记录: |