Send excel file for downloading GAE python
<p>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.</p>

<p>I found amount of topics in web, but any of them can't help me.
Related topics that I've tried to use: 1) <a href="http://stackoverflow.com/questions/7254317/how-can-i-store-excel-file-created-using-pyexcelerator-as-input-for-db-blobprope#">this is with Blobs, I tried at first</a>, 2) <a href="http://stackoverflow.com/questions/10490106/appengine-send-data-as-a-file-download">without Blob</a>, 3) <a href="http://stackoverflow.com/questions/10490106/appengine-send-data-as-a-file-download">with force-download MIME type</a>, also I've tried to use googlecloudstorage (can't find links to topics).</p>

<p>Here is my code:</p>

<pre><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'
</code></pre>

<p>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' </p>

<p>But the best response I've achieved is as on picture:</p>

<p><a href="http://i.stack.imgur.com/alwso.png" rel="nofollow"><img src="http://i.stack.imgur.com/alwso.png" alt=""></a></p>

<p>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!</p>

<p>Here is also my Request:</p>

<pre><code>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
</code></pre>

<p>and Response at debugger:</p>

<pre><code>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
</code></pre>

<p><strong>EDIT 1:</strong> (try answer by voscausa)</p>

<p><a href="http://i.stack.imgur.com/TUbMW.png" rel="nofollow"><img src="http://i.stack.imgur.com/TUbMW.png" alt="Response changed its format. I am going to try to write another data structure (not Sheet) to response"></a></p>

<p>Try this:</p>

<pre><code>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())
</code></pre>

python excel google-app-engine