简体   繁体   中英

Download CSV file to the client

I have the following route:

@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

which I'm calling with AJAX as follows:

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');
    },
  });
}

However, nothing's appearing on the client side. Am I missing something here?

Im more familiar with node than ajax ( i had the same issue in nodejs which i fixed here on stackoverflow), but maybe this will help:

$.ajax({
    async: false,

from How do I make jQuery wait for an Ajax call to finish before it returns?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM