【发布时间】:2020-01-24 10:57:52
【问题描述】:
我想将数据从 ajax 发送到烧瓶,并希望将此数据下载到用户端的 excel 文件中。 ajax 正在发送数据,我也可以打印这些数据,但烧瓶没有将这个 excel 文件发送到浏览器。 如果我获取任何数据,没有 ajax 烧瓶也可以工作并发送 excel 文件,但我想用 ajax 发布数据并获取 excel 文件。请帮助解决这个问题
**#ajax script**
$('#download').click(function() {
var date1 ={};
date1= window.fordownload;
alert(date1);
$.ajax({
url: '/downloads',
type: "POST",
data: date1,
contentType: "application/json; charset=utf-8",
error: function(e) {
console.log(e);
},
dataType: "json",
contentType: "application/json"
});
});
**#flask code**
@app.route('/downloads', methods=['GET','POST'])
def downloadData():
try:
json_data = request.get_json('date1')
print(json_data)
df_1 = pd.DataFrame(json_data)
print(df_1)
output = BytesIO()
writer = pd.ExcelWriter(output, engine='xlsxwriter')
df_1.to_excel(writer, startrow=0, merge_cells=False, sheet_name="Sheet_1", index=False)
workbook = writer.book
worksheet = writer.sheets["Sheet_1"]
format = workbook.add_format()
format.set_bg_color('#eeeeee')
worksheet.set_column(0, 9, 28)
writer.close()
output.seek(0)
return send_file(output, attachment_filename="testing.xlsx", as_attachment=True)
except Exception as e:
return (str(e))
它将数据从 ajax 发布到烧瓶,但是
【问题讨论】: