After researching, I know there is no way to send_file
and render_template
in one return; however, all of the other solutions pointed to a document that is stored on a server. Is there a workaround for a stream to send_file
and load a template either in the same page or another one?
app = Flask(__name__)
@app.route('/', methods=['GET','POST'])
def homes():
if request.method == 'POST':
document = request.files['test']
stream = io.BytesIO(document.read())
doc = aw.Document(stream)
# random doc manipulation
return send_file(doc, as_attachment=True, download_name='test.docx')
return render_template("index.html")
You can seperate the two routes like:
app.py
app = Flask(__name__)
@app.route('/', methods=['GET','POST'])
def homes():
return render_template("index.html") <- post a link to the `download_file` route in this template
@app.route('/download_file', methods=['GET'])
def download_file():
if request.method == 'GET'
document = request.files['test']
stream = io.BytesIO(document.read())
doc = aw.Document(stream)
return send_file(doc, as_attachment=True, download_name='test.docx')
Then in your html file use this:
index.html (example)
<!DOCTYPE html>
<html lang="en">
<body>
<ul>
<li><a href="{{ url_for('download_file') }}">Download</a>
</ul>
</body>
</html>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.