Mik*_*ann 7 javascript python jquery flask
我有这个烧瓶应用程序路线:
@app.route('/generatecleanbudgetfile', methods=['GET', 'POST'])
def clean_budget():
file = request.files.get('data_file')
app.logger.info('Budget Formatting request has started')
try:
if request.method == 'POST':
file = request.files.get('data_file')
file.seek(0)
buffer = budget_cleaner(file)
buffer.seek(0)
app.logger.info('Conversion Complete')
return send_file(
buffer,
as_attachment=True,
attachment_filename=f'stripped_budget_{dt.today().strftime("%m.%d.%Y")}.xlsx',
mimetype='application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
)
except:
app.logger.error(f"Budget formatting failed on {file}")
return render_template('error_type.html', title='Unable to process uploaded budget')
Run Code Online (Sandbox Code Playgroud)
我的HTML:
{% extends "base.html" %}
{% block head %}
{% endblock %}
{% block content %}
<div id="vo_budget_file_settings">
{# <a href="/generatecleanbudgetfile" class="btn btn-primary">Upload Final CRO Budget File</a> #}
<p id="uploadPara">Please upload the final CRO budget File</p>
<form class="" action="/generatecleanbudgetfile" method=POST enctype=multipart/form-data>
<input type="file" name="data_file" accept=".xls, .xlsx, .xlsm"/>
<input type="submit" value="Begin Format" onclick="loading();"/>
</form>
</div>
<!-- funtion to show css spinner on button click -->
<script type="text/javascript">
function loading(){
$(".loader").show();
}
</script>
Run Code Online (Sandbox Code Playgroud)
我知道 Flask 不能同时返回render_template
一个send_file
项目,因为它只能返回一个项目。
我该如何通过 JavaScript 下载文件,以便我可以使用 myreturn
来渲染新模板?
我想更换这件作品:
return send_file(
buffer,
as_attachment=True,
attachment_filename=f'stripped_budget_{dt.today().strftime("%m.%d.%Y")}.xlsx',
mimetype='application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
)
Run Code Online (Sandbox Code Playgroud)
Flask 无法在单个请求中同时完成这两项操作(渲染和发送文件)。
\n但你可以有替代解决方案\xe2\x86\x93
\n在您的generatecleanbudgetfile
要求中:
因此,当用户收到响应时,他们可以单击下载链接来检索第一步中生成的文件。
\n 归档时间: |
|
查看次数: |
907 次 |
最近记录: |