Python GoogleAppEngine下载一个包含文件的文件
好的,我有:Python GoogleAppEngine下载一个包含文件的文件,python,google-app-engine,download,Python,Google App Engine,Download,好的,我有: class Content(db.Model): code=db.TextProperty() 数据库中存储了3种不同的代码值。如何创建一个zip文件,将三个代码值存储在将要下载的三个单独的文件中 根据eric.f的回答: 我重写了他的代码,使其符合我的要求: contents = db.GqlQuery("SELECT * FROM Content ORDER BY created DESC") output = StringIO.StringIO()
class Content(db.Model):
code=db.TextProperty()
数据库中存储了3种不同的代码值。如何创建一个zip文件,将三个代码值存储在将要下载的三个单独的文件中
根据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)
然后做出响应,将output.getvalue()
设置为内容,并将标题设置如下:
Content-type: application/zip
Content-disposition: attachment; filename=test.zip
你能看一下我的代码看看你是否知道该怎么做吗?(
content.code
和content.code2
的值是格式化的python代码)我真的需要你的帮助。。。谢谢非常感谢。我知道了!您在代码中使用unicode吗?如果在写入zipfile之前不只是确保所有代码都是ascii码,请尝试使用str(code)而不是writestr()中的代码。writestr()代码包含用户提交的python代码
Content-type: application/zip
Content-disposition: attachment; filename=test.zip