我有以下路线:
@app.route('/download/', methods=['GET'])
def download():
output_filename = request.args.get('output_filename')
data = dict(foos=[1,2,3,3,2,1], bars=[7,7,7,7,7,7])
df = pd.DataFrame(data)
result = df.to_csv(index=False)
response = make_response(result)
response.headers['Content-Disposition'] = f'attachment; filename={output_filename}'
response.headers['Cache-Control'] = 'must-revalidate'
response.headers['Pragma'] = 'must-revalidate'
response.headers['Content-type'] = 'application/csv'
return response
我用AJAX调用的方法如下:
function download(data, handler = null) {
let data = {output_filename: "foo.csv"};
let url = "/download/";
$.ajax({
type: "GET",
url: url,
data: data,
success: function(data) {
console.log('success');
},
});
}
但是,客户端没有任何显示。我在这里想念什么吗?
最佳答案
我对节点的了解比对ajax更为熟悉(我在nodejs中遇到了相同的问题,已在stackoverflow上对其进行了修复),但这也许会有所帮助:
$.ajax({
async: false,
来自How do I make jQuery wait for an Ajax call to finish before it returns?
关于python - 将CSV文件下载到客户端,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/58886739/