使用Falcon RES API提供pdf文件

2024-10-02 20:32:42 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图使用falconrestapi提供一个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

我得到的是一个空的pdf文件,当然,我不能打开。我猜我没有在响应中加载文件内容,但我不是python方面的专家,我不知道该怎么做。你知道吗

如果我试试这个:

resp.stream, resp.stream_len = open(filename)

我得到这个错误:

UnicodeDecodeError: 'charmap' codec can't decode byte 0x8d in position 130: character maps to <undefined>

我完全肯定文件在那里。你知道吗


Tags: 文件selfstreamgetpdfondeffilename
1条回答
网友
1楼 · 发布于 2024-10-02 20:32:42

好了,搞定。我需要以二进制模式打开文件。我把它留在这里,以防有人需要这个例子,因为我自己找不到人。你知道吗

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

相关问题 更多 >