Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Decoding problems when returning xlsxwriter response with pyramid

I though this question would solve my problem, and I followed the Simple HTTP Server example but I'm getting different issues that I can't find a solution for.

I want to generate an Excel file in my server and return it to the user with an Http response. I'm using xlsxwriter to build the file and a pyramid framwork in my server. I've managed to build the file and return it, but then first issue is LibreCalc (I'm testing on Ubuntu 14) asks me how I want to import the file. It doesn't matter what I select I get this error message

General Error. General input/output error.

If I just build and save the file without returning it as a response, it opens fine.

My code to build 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 handle the response:

    $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();
    })

I thought at first it could have something to do with the fact that I use UTF-8 encoding in the python file, but since I can build and open the file otherwise, I don't think it does.

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

like image 538
Niel Avatar asked Nov 09 '22 05:11

Niel


1 Answers

I was able to get to a solution by following the answers to this question. I'm not sure exactly what did the trick, 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 perfectly, I only needed to change the http code. By the way I used angular-file-saver for the saveAs function.

like image 199
Niel Avatar answered Nov 14 '22 21:11

Niel