0

I've got an ajax call to a flask route. This ajax call is inside an onlick function of a button (because I have to send some preprocessed data). In the python code,I create a xlsx spreadhseet with 2 tabs and save the file in the server. Then I am trying to download it in the client but without success. No errors, and the response is 200 OK, but the file is not downloaded. This is my python code:

@app.route('/download_file', methods=['GET', 'POST'])
def download_file():
    v1 = request.json['v1']
    v2 = request.json['v2']
    v1_df = pd.DataFrame(v1, columns=V1_HEADERS)
    v2_df = pd.DataFrame(v2, columns=V2_HEADERS)
    final_excel = f'resources/final_excel.xlsx'
    writer = pd.ExcelWriter(final_excel, engine='xlsxwriter')
    v1_df.to_excel(writer, sheet_name='V1', index=False)
    v2_df.to_excel(writer, sheet_name='V2', index=False)
    writer.save()
    return send_file(final_excel, as_attachment=True, mimetype='application/vnd.openxmlformats-officedocument.spreadsheetml.sheet')

I also tried send_directory without success.

What am I doing wrong? I'm a little desperate.

Thank you very much in advance.

Solar
  • 445
  • 1
  • 4
  • 12

1 Answers1

0

It seems to me as though you may not be handling the returned file in your Ajax callback correctly. Since this is an Ajax call it will be returned to the relevant callback for you to handle.

So in your .JS code like below handle it as you wish, e.g.

 success:function(callback){
    var temp = 'file-> ' + callback;
    $(doc).text(temp)
 }

This may also help - Download a file

AzyCrw4282
  • 7,222
  • 5
  • 19
  • 35