繁体   English   中英

发送Excel文件以下载GAE python

[英]Send excel file for downloading GAE python

我正在将Google App Engine与python 2.7结合使用。 并且需要生成内存中的xls文件并将其发送给用户以进行下载。

我在网络上发现了很多主题,但是其中任何一个都帮不了我。 我尝试使用的相关主题:1) 这是在Blobs中,我最初尝试过 ; 2)在没有Blob时 ,3) 具有强制下载MIME类型 ,也尝试过使用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'

问题在于发送响应(而不是创建文件)。 我尝试了不同的“ Content-Type”:“ application / vnd.ms-excel”,“ application / download”,“ application / force-download”,“ application / octet-stream”,“ application / vnd.openxmlformats-officedocument”。电子表格ml.sheet'

但是我所获得的最佳响应如下图所示:

我无法强制我的浏览器开始从服务器下载数据。 我想我的请求中可能有一些内容应该告诉服务器“嘿,我要下载”,但这只是我的想法,我没有找到任何关于此的信息。 将不胜感激!

这也是我的要求:

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

和调试器的响应:

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回答)

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

尝试这个:

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