发送excel文件下载GAE python

2024-10-01 04:49:48 发布

您现在位置:Python中文网/ 问答频道 /正文

我使用的是带有python2.7的googleappengine。需要在内存中生成xls文件并发送给用户下载。在

我在网上找到了很多话题,但都帮不了我。 我尝试过使用的相关主题:1)this is with Blobs, I tried at first,2)without Blob,3)with force-download MIME type,我也尝试过使用googlecloudstorage(找不到指向主题的链接)。在

这是我的代码:

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'

问题在于发送响应(而不是创建文件)。我尝试了不同的“内容类型”: '应用程序/越南盾-excel', “应用程序/下载”, “应用程序/强制下载”, '应用程序/八位字节流', '应用程序/vnd.openxml格式- officedocument.spreadsheetml.sheet'

但我得到的最好的回应是:

无法从我的服务器下载数据。我想我应该在那里找到我的想法,但是我想我应该在那里找到一些东西。谢谢你的帮助!在

这也是我的要求:

^{pr2}$

和调试器上的响应:

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

编辑1:(请尝试voscausa回答)

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


Tags: 文件toself应用程序主题responsedownloadcontent
1条回答
网友
1楼 · 发布于 2024-10-01 04:49:48

试试这个:

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

相关问题 更多 >