我正在尝试启动单页烧瓶应用程序,允许用户下载word文档.我已经想出了如何使用python-docx制作/保存文档,但现在我需要在响应中提供文档.有任何想法吗?
这是我到目前为止所拥有的:
from flask import Flask, render_template
from docx import Document
from cStringIO import StringIO
@app.route('/')
def index():
document = Document()
document.add_heading("Sample Press Release", 0)
f = StringIO()
document.save(f)
length = f.tell()
f.seek(0)
return render_template('index.html')
Run Code Online (Sandbox Code Playgroud) 当我在 Azure 上部署 Flask 应用程序时,视图会引发TypeError: send_from_directory() missing 1 required positional argument: 'path'. 当我在本地运行时,这不会发生。
from flask import send_from_directory
@app.route('/download/<path:filename>', methods=['GET', 'POST'])
def download(filename):
uploads = os.path.join(app.root_path, app.config['UPLOAD_FOLDER'])
return send_from_directory(directory=uploads, filename=filename)
Run Code Online (Sandbox Code Playgroud)