我有以下路线:
@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');
},
});
}
但是,客户端上什么也没有出现。我在这里遗漏了什么吗?
最佳答案
我对node比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/