通过CherryPy

时间:2017-03-15 12:58:02

标签: python-3.x cherrypy python-pptx

我试图让用户在访问pge时下载pptx文件,让我们说:http://127.0.0.1:8080/download

以下是我的代码片段:

from pptx import Presentation
from cherrypy.lib.static import serve_file

@cherrypy.expose
def download(self):
    prs = Presentation()
    title_slide_layout = prs.slide_layouts[0]
    slide = prs.slides.add_slide(title_slide_layout)
    title = slide.shapes.title
    subtitle = slide.placeholders[1]
    title.text = "Hello, World!"
    subtitle.text = "python-pptx was here!"
    pptx = prs.save('test.pptx')
    return serve_file(path, "application/x-download", "attachment")

我真的不明白serve_file是如何工作的,并且想知道是否有权这样做。 我是CherryPy的初学者 希望你能帮助我。

1 个答案:

答案 0 :(得分:0)

只是一个更新。我使用过static.serve_file并且它有效!

return static.serve_file(path, "application/x-download",
                             "attachment", name =os.path.basename(path))