【问题标题】:Pyramid FileResponse for dynamic filesPyramid FileResponse 动态文件
【发布时间】:2020-05-12 23:30:42
【问题描述】:

我希望我的客户通过金字塔下载(而不是渲染)动态生成的 PDF 文件。现在我这样做:

def get_pdf(request):
    pdfFile = open('/tmp/example.pdf', "wb")
    pdfFile.write(generator.GeneratePDF())

    response = FileResponse('/tmp/example.pdf')
    response.headers['Content-Disposition'] = ('attachment;  filename=example.pdf')
    return response

从客户的角度来看,这正是我所需要的。然而,

  1. 它留下了一个孤立的文件
  2. 它不是线程安全的(尽管我可以使用随机文件名)

docs 说:

类文件响应

一个响应对象,可用于简单地从磁盘提供静态文件。

所以FileResponse 可能不是我应该使用的。您如何将其替换为更具动态但对客户而言难以区分的内容?

【问题讨论】:

    标签: pyramid


    【解决方案1】:

    只需使用具有相同标头的正常响应即可:

    def get_pdf(request):
        response = Response(body=generator.GeneratePDF())
        response.headers['Content-Disposition'] = ('attachment;filename=example.pdf')
        return response
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-07-28
      • 2021-02-19
      • 1970-01-01
      • 1970-01-01
      • 2022-07-06
      • 1970-01-01
      • 2011-09-28
      • 2019-05-31
      相关资源
      最近更新 更多