0

I am using Google App Engine with python 2.7. And there is need to generate in-memory xls-file and send it to user for downloading.

I found amount of topics in web, but any of them can't help me. Related topics that I've tried to use: 1) this is with Blobs, I tried at first, 2) without Blob, 3) with force-download MIME type, also I've tried to use googlecloudstorage (can't find links to topics).

Here is my code:

import StringIO

class ExcelHandler(BaseHandler):

def post(self):

    """Save members to excel document and send to user"""

    sheet = pyexcel.Sheet([[1, 2], [3, 4]])
    filesheet = StringIO.StringIO()
    sheet.save_to_memory('xls', filesheet)
    filesheet.close()

    self.response.write(sheet)
    self.response.headers['Content-Type'] = 'application/force-download'
    self.response.headers['Content-Transfer-Encoding'] = 'utf-8'
    self.response.headers['Content-Disposition'] = 'attachment; filename=test.xlsx'

The problem is in sending response (not in creating file). I tried different 'Content-Type': 'application/vnd.ms-excel', 'application/download', 'application/force-download', 'application/octet-stream', 'application/vnd.openxmlformats - officedocument.spreadsheetml.sheet'

But the best response I've achieved is as on picture:

I can't enforce my browser to start downloading data from server. I guess there may be something in my Request that should say to server 'Hey, I want to download', but it is only my thoughts, I've not found anything about that. Will appreciate any help!

Here is also my Request:

POST /reg/excel HTTP/1.1
Host: 0.0.0.0:8080
Connection: keep-alive
Content-Length: 0
Accept: */*
Origin: http://0.0.0.0:8080
X-Requested-With: XMLHttpRequest
User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML,   like Gecko) Chrome/51.0.2704.106 Safari/537.36
Referer: http://0.0.0.0:8080/competition?dbKey=agpkZXZ- dG1tb3NjchgLEgtDb21wZXRpdGlvbhiAgICAgICgCww
Accept-Encoding: gzip, deflate
Accept-Language: ru-RU,ru;q=0.8,en-US;q=0.6,en;q=0.4

and Response at debugger:

HTTP/1.1 200 OK
content-disposition: attachment; filename=test.xlsx
content-transfer-encoding: utf-8
cache-control: no-cache
content-type: application/force-download
Content-Length: 64
Server: Development/2.0
Date: Sun, 02 Oct 2016 15:36:20 GMT

EDIT 1: (try answer by voscausa)

Response changed its format. I am going to try to write another data structure (not Sheet) to response

Community
  • 1
  • 1

1 Answers1

1

Try this:

output = StringIO.StringIO()
.......         

self.response.headers[b'Content-Type'] = b'application/vnd.ms-excel; charset=utf-8'
self.response.headers[b'Content-Disposition'] = b'attachment; filename=test.xlsx'
self.response.write(output.getvalue())
voscausa
  • 11,253
  • 2
  • 39
  • 67
  • `Content-Type` is the key bit here. – Dave W. Smith Oct 02 '16 at 22:45
  • @voscause, I tried your answer, and I get different response (look at EDIT 1). Guess it may be my mistake in save_to_memory method. I am going to check what should I write to response (Sheet/Book/etc.) and add comment if it will help. – Daria Plotnikova Oct 03 '16 at 16:16