gpt4 book ai didi

javascript - Flask send_file 作为附件没有响应

转载 作者:行者123 更新时间:2023-12-03 01:36:09 26 4
gpt4 key购买 nike

我用flask编写了一个jQuery,点击它应该执行SQL搜索并将数据帧导出为ex​​cel,脚本是:

<script type=text/javascript>
$(function () {
$('a#export_to_excel').bind('click', function () {
$.getJSON($SCRIPT_ROOT + ' /api/sanctionsSearch/download', {
nm: $('input[name="nm"]').val(),
searchtype: $('select[name="searchtype"]').val()
}, function (data) {
$("#download_results").text(data.result);
});
return false;
});
});

但是浏览器没有响应,我的python代码如下:

from io import BytesIO,StringIO
from flask import render_template, request, url_for, jsonify, redirect, request, Flask, send_file

def index():

#get the dataframe ready and define as 'data', parameters obtained from form input in html

name = request.args.get('nm','', type = str)
type = request.args.get('searchtype','Entity',type = str)

#function get_entity() to get the dataframe
#I have checked and the dataframe is functioning properly
data = get_entity(name,type)

#check if the dataframe is empty
if data.empty == True:
print("its not working bruh...")
word = "No results to export! Please try again!"
return jsonify(result = word)

#store the csv to BytesIO
proxy = StringIO()
data.to_csv(proxy)
mem = BytesIO()
mem.write(proxy.getvalue().encode('utf-8'))

mem.seek(0)
proxy.close()

print("download starting....")

#send file
send_file(mem, as_attachment=True,attachment_filename='Exportresults.csv', mimetype='text/csv')
word = "Download starting!"
return jsonify(result = word)

有人可以告诉我我的代码有什么问题吗? “下载开始...”已正确打印到 html,但下载根本没有开始。

最佳答案

解决方案并不理想,但我所做的是在 jquery 中添加一个 window.open(url) 命令,该命令将调用另一个函数,该函数会将 send_file 发送给用户。

关于javascript - Flask send_file 作为附件没有响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51060433/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com