简体   繁体   English

发送Excel文件以下载GAE python

[英]Send excel file for downloading GAE python

I am using Google App Engine with python 2.7. 我正在将Google App Engine与python 2.7结合使用。 And there is need to generate in-memory xls-file and send it to user for downloading. 并且需要生成内存中的xls文件并将其发送给用户以进行下载。

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). 我尝试使用的相关主题:1) 这是在Blobs中,我最初尝试过 ; 2)在没有Blob时 ,3) 具有强制下载MIME类型 ,也尝试过使用googlecloudstorage(找不到链接)主题)。

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' 我尝试了不同的“ Content-Type”:“ application / vnd.ms-excel”,“ application / download”,“ application / force-download”,“ application / octet-stream”,“ application / vnd.openxmlformats-officedocument”。电子表格ml.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) 编辑1 :(尝试通过voscausa回答)

响应更改了其格式。我将尝试编写另一个数据结构(而非工作表)以进行响应

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())

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM