Decoding problems when returning an xlsxwriter pyramid response

I, although this question would solve my problem, and I followed the Simple HTTP Server , but I am having different problems due to which I can not find a solution.

I want to create an Excel file on my server and return it to the user with an Http response. I use xlsxwriter to create a pyramid file and framework on my server. I managed to create the file and return it, but then the first problem is LibreCalc (I am testing Ubuntu 14), asking how I want to import the file. No matter what I choose, I get this error message

Common mistake. General input / output error.

If I just create and save the file without returning it as an answer, it opens perfectly.

My code to create the file:

output = StringIO() workbook = xlsxwriter.Workbook(output) worksheet = workbook.add_worksheet() # add the data workbook.close() excelcontent = output.getvalue() response = Response(content_type='application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', body=excelcontent) response.headers.add('Content-Disposition', "attachment; filename=%s.xlsx" % nice_filename) response.headers.add('Access-Control-Expose-Headers','Content-Disposition') response.headers.add("Content-Length", str(len(excelcontent))) response.headers.add('Last-Modified', last_modified) response.headers.add("Cache-Control", "no-store") response.headers.add("Pragma", "no-cache") return response 

And I process the answer:

  $http({ method: 'POST', url: url, data: data}, {responseType: 'arraybuffer'} ).success(function (response, status, headers, config) { var file = new Blob([response], {type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'}); var fileURL = URL.createObjectURL(file); var result = document.getElementsByClassName("excel_hidden_download"); var anchor = angular.element(result); var filename_header = headers('Content-Disposition'); var filename = filename_header.split('filename=')[1]; anchor.attr({ href: fileURL, target: '_blank', download: filename })[0].click(); }) 

At first I thought that this could be due to the fact that I use UTF-8 encoding in the python file, but since I can create and open the file otherwise, I do not think so.

# -*- coding: utf-8 -*-

+5
source share
1 answer

I managed to find a solution by completing the answers to this question. I'm not sure what the trick is, but here is my resulting code:

  $http({ method: 'POST', url: url, data: data, responseType: 'arraybuffer' }).success(function (response, status, headers, config) { var blob = new Blob([response], { type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' }); var filename = headers('Content-Disposition').split('filename=')[1]; var config = { data: blob, filename: filename, }; FileSaver.saveAs(config); }) 

And it works fine, I only need to change the http code. By the way, I used angular-file-saver for the saveAs function.

+1
source

All Articles