5

I am trying to download an Excel file created by XlsxWriter by sending it as a response from Flask view to AngularJS. However, when I try to open the file, it says that the file is corrupted. I do not know what I am missing here. Please find my code below:

XlsGenerator.py:

    output = io.BytesIO()
    workbook = xlsxwriter.Workbook(output, {'in_memory': True})
        # code to add data 
    workbook.close()
    output.seek(0)
    response = Response(output.getvalue(), mimetype='application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
                    headers={"Content-Disposition": "attachment;filename=Dss_project.txt"},
                    content_type='application/vnd.openxmlformats-officedocument.spreadsheetml.sheet')

    return response

app.js:

   $http(
        {
           method: 'POST',
           url: '/export',  /*You URL to post*/
           data: data,
           headers: {
            'Content-Type': 'application/json',
            'Accept': 'application/vnd.openxmlformats 
                        officedocument.spreadsheetml.sheet'
            }
            }).then(function(response) {
            var blob = new Blob([response.data], 
                                {type:'application/vnd.openxmlformats- 
                                      officedocument.spreadsheetml.sheet'});
            FileSaver.saveAs(blob, 'project_data.xlsx')
            }, function(response) {
            $window.alert('Export failed');
        });

Also tried using the send_file() as an alternative, but it gave the same error!

send_file(output, attachment_filename='project_data.xlsx',as_attachment=True)
mia
  • 51
  • 5

0 Answers0