[英]google app engine download a file containing files
好的,我有:
class Content(db.Model):
code=db.TextProperty()
并且数据库中存储了3种不同的代码值。 如何创建一个zip文件,将三个代码值存储在3个单独的文件中,这些文件将被下载?
基于eric.f的答案:我重写了他的代码,使其可以执行我想要的操作:
contents = db.GqlQuery("SELECT * FROM Content ORDER BY created DESC")
output = StringIO.StringIO()
with zipfile.ZipFile(output, 'w') as myzip:
for content in contents:
if content.code:
code=content.code
else:
code=content.code2
myzip.writestr('udacity_code'+`content.key().id()`, code)
self.response.headers["Content-Type"] = "application/zip"
self.response.headers['Content-Disposition'] = "attachment; filename=test.zip"
self.response.out.write(output.getvalue())
我虽然出错了...
self.response.out.write(output.getvalue(), "utf-8")
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/StringIO.py", line 270, in getvalue
UnicodeDecodeError: 'ascii' codec can't decode byte 0xb4 in position 10: ordinal not in range(128)
import zipfile
import StringIO
output = StringIO.StringIO()
with zipfile.ZipFile(output, 'w') as myzip:
myzip.writestr('file1.txt', 'aaaaaaaaa')
myzip.writestr('file2.txt', 'bbbbbbbbb')
myzip.writestr('file3.txt', 'ccccccccc')
然后做出响应,将output.getvalue()
设置为内容,并设置标头,如下所示:
Content-type: application/zip
Content-disposition: attachment; filename=test.zip
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.